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.demo.annotation.DemoMode;
import com.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.core.excel.ExcelOptions;
import com.jeeplus.core.excel.utils.EasyPoiUtil;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.test.reimbursementAccountant.domain.dto.AccountantReimbursementBusinessDTO;
import com.jeeplus.test.reimbursementAssess.domain.dto.AssessReimbursementBusinessDTO;
import com.jeeplus.test.reimbursementAssess.domain.dto.AssessReimbursementBusinessReturnDTO;
import com.jeeplus.test.reimbursementAssess.domain.dto.AssessReimbursementDTO;
import com.jeeplus.test.reimbursementAssess.domain.dto.AssessReimbursementUserDTO;
import com.jeeplus.test.reimbursementAssess.service.AssessReimbursementBusinessService;
import com.jeeplus.test.reimbursementsys.domain.dto.ReimbursementBusinessDTO;
import com.jeeplus.test.reimbursementsys.domain.dto.ReimbursementBusinessReturnDTO;
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.util.Map;

/**
 * @author 徐滕
 * @version 1.0.0
 * @ClassName ReimbursementUserController.java
 * @Description TODO 报销人员年份金额信息
 * @createTime 2022年05月16日 10:35:21
 */
@RestController
@RequestMapping("/assess/reimbursementBusiness")
public class AssessReimbursementBusinessController {

    @Autowired
    private AssessReimbursementBusinessService service;

    /**
     * 根据id报销人员信息
     * @param id
     * @return
     */
    @ApiLog("根据id报销人员信息")
    @GetMapping("queryById")
    public ResponseEntity queryById(@RequestParam String id) {
        AssessReimbursementBusinessDTO reimbursementDTO = service.queryById (id);
        return ResponseEntity.ok (reimbursementDTO);
    }

    /**
     * 根据id报销人员信息
     * @param id
     * @return
     */
    @ApiLog("根据id报销人员信息")
    @PreAuthorize("hasAnyAuthority('assess:reimbursementBusiness:add')")
    @GetMapping("getBusinessById")
    public ResponseEntity getBusinessById(@RequestParam String id) {
        AssessReimbursementDTO reimbursementDTO = service.getBusinessById (id);
        return ResponseEntity.ok (reimbursementDTO);
    }

    /**
     * 根据人员id查询报销年份信息
     * @param reimbursementUser
     * @param page
     * @return
     */
    @ApiLog("查询角色列表")
    @GetMapping("getYearList")
    public ResponseEntity<IPage<AssessReimbursementUserDTO>> getYearList(AssessReimbursementUserDTO reimbursementUser, Page<AssessReimbursementUserDTO> page) {
        IPage<AssessReimbursementUserDTO> list = service.getYearList(page,reimbursementUser);
        return ResponseEntity.ok (list);
    }


    /**
     * 查询报销信息
     * @return
     */
    @ApiLog("查询报销人员信息列表")
    @GetMapping("findList")
    public ResponseEntity findList(AssessReimbursementUserDTO reimbursementUser, Page<AssessReimbursementUserDTO> page) {
        IPage<AssessReimbursementUserDTO> list = service.findList(page,reimbursementUser);
        return ResponseEntity.ok(list);
    }

    /**
     * 保存业务报销编码
     * @return
     */
    @ApiLog("保存报销人员信息")
    @PostMapping("save")
    public ResponseEntity save(@RequestBody AssessReimbursementBusinessDTO reimbursementUser) {
        Map<String, Object> result = service.save(reimbursementUser);
        Boolean success = (Boolean) result.get("success");
        if(success){
            return ResponseEntity.ok(result.get("message"));
        }
        return ResponseEntity.badRequest ().body (result.get("message"));
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DemoMode
    @ApiLog("批量删除")
    @PreAuthorize("hasAuthority('assess:reimbursementBusiness:del')")
    @DeleteMapping("delete")
    public ResponseEntity delete(String ids) {
        String idArray[] =ids.split(",");
        service.delete ( Lists.newArrayList (idArray) );
        return ResponseEntity.ok ("删除成功！");
    }




    /**
     * 查询报销信息
     * @return
     */
    @ApiLog("查询业务报销信息列表")
    @PreAuthorize("hasAuthority('assess:reimbursementBusiness:view')")
    @GetMapping("findListByBusinessCodeId")
    public ResponseEntity findListByBusinessCodeId(AssessReimbursementBusinessDTO reimbursementBusiness) {
        if(StringUtils.isBlank(reimbursementBusiness.getBusinessCodeId())){
            return ResponseEntity.badRequest ().body ("请先选择业务信息");
        }
        AssessReimbursementBusinessReturnDTO info = service.findListByBusinessCodeId(reimbursementBusiness);
        return ResponseEntity.ok(info);
    }


    /**
     * 导出中审众环人员信息数据
     * @param reimbursementBusiness
     * @param response
     * @throws Exception
     */
    @ApiLog("人员报销详情导出")
    @GetMapping("exportBusinessExcel")
    @ApiOperation(value = "人员报销详情导出Excel")
    public void exportBusinessExcel(AssessReimbursementBusinessDTO reimbursementBusiness, ExcelOptions options, HttpServletResponse response) {
        String fileName = options.getFilename ( );
        AssessReimbursementBusinessReturnDTO info = service.findListByBusinessCodeId(reimbursementBusiness);
        for (AssessReimbursementBusinessDTO businessInfo : info.getList()) {
            if(StringUtils.isNotBlank(businessInfo.getReimbursementAddress())){
                switch (businessInfo.getReimbursementAddress()){
                    case "1":
                        businessInfo.setReimbursementAddress("总所报销");
                        break;
                    case "0":
                        businessInfo.setReimbursementAddress("分所报销");
                        break;
                    default:
                        businessInfo.setReimbursementAddress("——");
                        break;

                }
            }else{
                businessInfo.setReimbursementAddress("——");
            }
            businessInfo.setBusinessCode(info.getBusinessCode());
        }

        EasyPoiUtil.exportExcel ( info.getList(), info.getYear() + "年【" + info.getBusinessCode() + "】" + "批次报销详情",  options.getSheetName ( ), AssessReimbursementBusinessDTO.class, fileName, response );
    }
}
