package com.jeeplus.test.reimbursementAccountant.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.core.excel.ExcelOptions;
import com.jeeplus.core.excel.ExportMode;
import com.jeeplus.core.excel.utils.EasyPoiUtil;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.test.reimbursementAccountant.domain.dto.AccountantReimbursementBusinessTwoDTO;
import com.jeeplus.test.reimbursementAccountant.domain.dto.AccountantReimbursementDTO;
import com.jeeplus.test.reimbursementAccountant.service.AccountantReimbursementBusinessService;
import com.jeeplus.test.reimbursementAccountant.service.AccountantReimbursementBusinessTwoService;
import com.jeeplus.test.reimbursementProject.domain.dto.ProjectReimbursementBusinessTwoDTO;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/accountant/reimbursementBusinessTwo")
public class AccountantReimbursementBusinessTwoController {

    @Autowired
    private AccountantReimbursementBusinessTwoService accountantReimbursementBusinessTwoService;
    @Autowired
    private AccountantReimbursementBusinessService reimbursementBusinessService;

    @ApiLog("查询无票报销列表")
    @RequestMapping("findListPage")
    public ResponseEntity findListPage(AccountantReimbursementBusinessTwoDTO accountantReimbursementBusinessTwoDTO,Page<AccountantReimbursementBusinessTwoDTO> page){
        IPage<AccountantReimbursementBusinessTwoDTO> listPage = accountantReimbursementBusinessTwoService.findListPage(page, accountantReimbursementBusinessTwoDTO);
        return ResponseEntity.ok(listPage);
    }

    @ApiLog("根据id物理删除")
    @RequestMapping("delById/{id}")
    @PreAuthorize("hasAuthority('accountant:reimbursementBusinessTwo:del')")
    public ResponseEntity delById(@PathVariable("id")String ids){
        String idArray[] =ids.split(",");
        accountantReimbursementBusinessTwoService.deleteByList ( Lists.newArrayList (idArray) );
        return ResponseEntity.ok("删除成功！");
    }

    @ApiLog("新增")
    @RequestMapping("addData")
    @PreAuthorize("hasAuthority('accountant:reimbursementBusinessTwo:add')")
    public ResponseEntity addData(@RequestBody AccountantReimbursementBusinessTwoDTO accountantReimbursementBusinessTwoDTO){
        accountantReimbursementBusinessTwoService.addData(accountantReimbursementBusinessTwoDTO);
        return ResponseEntity.ok("添加成功");
    }

    @ApiLog("下拉列表")
    @RequestMapping("findSelectList")
    public ResponseEntity findSelectList(){
        return ResponseEntity.ok(accountantReimbursementBusinessTwoService.findSelectList());
    }

    @ApiLog("绑定")
    @RequestMapping("modifyBindCode")
    @PreAuthorize("hasAuthority('accountant:reimbursementBusinessTwo:binding')")
    public ResponseEntity modifyBindCode(@RequestParam("ids")String ids,@RequestParam("businessCode")String businessCode){
        String[] split = ids.split(",");
        BigDecimal sum = new BigDecimal(0);
        if(split.length!=0){
            for (int i = 0; i < split.length; i++) {
                AccountantReimbursementBusinessTwoDTO dataById = accountantReimbursementBusinessTwoService.findDataById(split[i]);
                sum = sum.add(new BigDecimal(dataById.getReimbursementAmount()));
            }
        }else{
            AccountantReimbursementBusinessTwoDTO dataById = accountantReimbursementBusinessTwoService.findDataById(ids);
            sum = new BigDecimal(dataById.getReimbursementAmount());
        }
        AccountantReimbursementDTO businessById = reimbursementBusinessService.getBusinessById(businessCode);
        if(new BigDecimal(businessById.getSurplusReimbursementAmount()).compareTo(sum)<0){
            return ResponseEntity.ok(false);
        }
        if(split.length!=0){
            for (int i = 0; i < split.length; i++) {
                accountantReimbursementBusinessTwoService.modifyBindCode(split[i],businessCode);
            }
        }else{
            accountantReimbursementBusinessTwoService.modifyBindCode(ids,businessCode);
        }

        return ResponseEntity.ok("成功");
    }

    @ApiLog("展示下拉列表所有年份")
    @RequestMapping("findSelectYearList")
    public ResponseEntity findSelectYearList(){
        return ResponseEntity.ok(accountantReimbursementBusinessTwoService.findSelectYearList());
    }

    @ApiLog("根据年份展示所有编号")
    @RequestMapping("findSelectCodeListByYear/{year}")
    public ResponseEntity findSelectCodeListByYear(@PathVariable("year")String year){
        return ResponseEntity.ok(accountantReimbursementBusinessTwoService.findSelectCodeListByYear(year));
    }


    /**
     * 导出兴光会计无票报销信息数据
     * @param reimbursementUserDTO
     * @param page
     * @param response
     * @throws Exception
     */
    @ApiLog("导出兴光会计无票报销信息数据")
    @GetMapping("export")
    @ApiOperation(value = "导出兴光会计无票报销信息数据excel")
    public void exportFile(AccountantReimbursementBusinessTwoDTO reimbursementUserDTO, Page <AccountantReimbursementBusinessTwoDTO> page, ExcelOptions options, HttpServletResponse response) {
        String fileName = options.getFilename ( );

        List<AccountantReimbursementBusinessTwoDTO> result;
        if ( ExportMode.current.equals ( options.getMode ( ) ) ) {
            result = accountantReimbursementBusinessTwoService.findListPage ( page, reimbursementUserDTO ).getRecords();
        } else if ( ExportMode.selected.equals ( options.getMode ( ) ) ) {
            result = accountantReimbursementBusinessTwoService.findListPage ( page, reimbursementUserDTO ).getRecords().stream ( ).filter ( reimbursement ->
                    options.getSelectIds ( ).contains ( reimbursement.getId ( ) )
            ).collect ( Collectors.toList ( ) );
        } else {
            page.setSize ( -1 );
            page.setCurrent ( 0 );
            result = accountantReimbursementBusinessTwoService.findListPage ( page, reimbursementUserDTO ).getRecords();
        }
        EasyPoiUtil.exportExcel ( result, "兴光会计无票报销信息数据",  options.getSheetName ( ), AccountantReimbursementBusinessTwoDTO.class, fileName, response );
    }
}
