package com.jeeplus.test.reimbursementsys.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.reimbursementsys.domain.dto.ReimbursementBusinessTwoDTO;
import com.jeeplus.test.reimbursementsys.domain.dto.ReimbursementDTO;
import com.jeeplus.test.reimbursementsys.service.ReimbursementBusinessService;
import com.jeeplus.test.reimbursementsys.service.ReimbursementBusinessTwoService;
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("/reimbursementBusinessTwo")
public class ReimbursementBusinessTwoController {

    @Autowired
    private ReimbursementBusinessTwoService reimbursementBusinessTwoService;
    @Autowired
    private ReimbursementBusinessService reimbursementBusinessService;

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

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

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

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

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

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

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

    @ApiLog("根据年份展示所有编号")
    @RequestMapping("findSelectCodeListByYear")
    public ResponseEntity findSelectCodeListByYear(String selectedYear, String reimbursementAddress){
        return ResponseEntity.ok(reimbursementBusinessTwoService.findSelectCodeListByYear(selectedYear, reimbursementAddress));
    }


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

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