package com.jeeplus.test.reimbursementAssess.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.test.reimbursementAccountant.domain.dto.AccountantReimbursementBusinessTwoDTO;
import com.jeeplus.test.reimbursementAssess.domain.dto.AssessReimbursementBusinessTwoDTO;
import com.jeeplus.test.reimbursementAssess.domain.dto.AssessReimbursementDTO;
import com.jeeplus.test.reimbursementAssess.service.AssessReimbursementBusinessService;
import com.jeeplus.test.reimbursementAssess.service.AssessReimbursementBusinessTwoService;
import io.swagger.annotations.ApiOperation;
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.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/assess/reimbursementBusinessTwo")
public class AssessReimbursementBusinessTwoController {

    @Autowired
    private AssessReimbursementBusinessTwoService assessReimbursementBusinessTwoService;
    @Autowired
    private AssessReimbursementBusinessService reimbursementBusinessService;

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

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

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

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

    @ApiLog("绑定")
    @RequestMapping("modifyBindCode")
    @PreAuthorize("hasAuthority('assess: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++) {
                AssessReimbursementBusinessTwoDTO dataById = assessReimbursementBusinessTwoService.findDataById(split[i]);
                sum = sum.add(new BigDecimal(dataById.getReimbursementAmount()));
            }
        }else{
            AssessReimbursementBusinessTwoDTO dataById = assessReimbursementBusinessTwoService.findDataById(ids);
            sum = new BigDecimal(dataById.getReimbursementAmount());
        }
        AssessReimbursementDTO 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++) {
                assessReimbursementBusinessTwoService.modifyBindCode(split[i],businessCode);
            }
        }else{
            assessReimbursementBusinessTwoService.modifyBindCode(ids,businessCode);
        }

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

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

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


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

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