package com.dayou.controller;

import cn.hutool.core.util.ObjectUtil;
import com.dayou.annotation.DataPermission;
import com.dayou.annotation.IgnoreAuth;
import com.dayou.dto.FinanceRealFundExportDTO;
import com.dayou.dto.RealFundAssetsStatDTO;
import com.dayou.entity.FinanceRealFund;
import com.dayou.exception.ErrorCode;
import com.dayou.utils.LoginContext;
import com.dayou.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RestController;
import com.dayou.service.IFinanceClaimService;
import com.dayou.entity.FinanceClaim;
import com.dayou.common.RestResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static com.dayou.common.Constants.FINANCE_DEPARTMENT;
import static com.dayou.common.Constants.MANAGER_OFFICE;

/**
 * 实收款认领
 *
 * @author wucl
 * @since 2023-12-08
 */
@RestController
@RequestMapping("financeClaim")
@Slf4j
public class FinanceClaimController extends BaseController {
    @Autowired
    private IFinanceClaimService financeClaimService;

    /**
    * 实收款认领列表
    */
    @GetMapping("")
    public RestResponse<Page<FinanceClaim>> page(FinanceClaim financeClaim, Page page){
        Page<FinanceClaim> pages=financeClaimService.selectPage(page,financeClaim);
        return RestResponse.data(pages);
    }

    /**
     * 实收款认领详情
     */
    @GetMapping("/{id}")
    public RestResponse<FinanceClaim> detail(@PathVariable Long id){
        FinanceClaim xFinanceClaim =financeClaimService.detail(id);
        return RestResponse.data(xFinanceClaim);
     }

    /**
     * 实收款认领新增
     */
    @PostMapping("")
    public RestResponse<Boolean> save(@RequestBody @Valid FinanceClaim financeClaim) {
        Boolean ret = financeClaimService.add(financeClaim);
        return RestResponse.data(ret);
    }

    /**
     * 实收款认领更新
     */
    @PutMapping("")
    public RestResponse<Boolean> update(@RequestBody FinanceClaim financeClaim) {
        Boolean ret = financeClaimService.update(financeClaim);
        return RestResponse.data(ret);
    }

    /**
     * 实收款认领删除
     */
    @DeleteMapping("/{id}")
    public RestResponse<Boolean> delete(@PathVariable Long id) {
        Boolean ret = financeClaimService.delete(id);
        return RestResponse.data(ret);
    }

    /**
     * 某实收款认领列表
     */
    @GetMapping("/list/{id}")
    public RestResponse<List<FinanceClaimVO>> getList(@PathVariable Long id){
        List<FinanceClaimVO> list = financeClaimService.getList(id);
        return RestResponse.data(list);
    }

    /**
     * 个贷认领模板下载
     * @param request
     * @param response
     */
    @IgnoreAuth
    @GetMapping("/personal/claim/template")
    public void downloadPersonalClaimTemplate(HttpServletRequest request, HttpServletResponse response){
        try {
            XSSFWorkbook workbook = new XSSFWorkbook(new ClassPathResource("xlsx/personal/personal_claim_template.xlsx").getInputStream());
            writeXss2Response(request,response,workbook,"个贷认领模板.xlsx");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 个贷模板认领实收款
     * @param file
     * @return
     */
    @PostMapping("/personal/temp/upload/{financeFundId}")
    public RestResponse<Boolean> personalClaimByTemplate(@RequestPart("file") MultipartFile file,@PathVariable("financeFundId") Long financeFundId){
        Boolean ret = financeClaimService.personalClaimByTemplate(file,financeFundId);
        return RestResponse.data(ret);
    }

    /**
     * 大中型实收款统计列表
     */
    @GetMapping("/major/stat")
    public RestResponse<Page<RealFundMajorStatVO>> majorStat(RealFundMajorStatVO majorStat, Page page,String keyword){
        Page<RealFundMajorStatVO> pages=financeClaimService.majorStat(page,majorStat,keyword);
        return RestResponse.data(pages);
    }

    /**
     * 大中型实收款统计列表 导出
     */
    @GetMapping("/major/stat/export")
    public void majorStatExport(RealFundMajorStatVO majorStat, HttpServletResponse response,String keyword) throws IOException {
        List<RealFundMajorStatVO> list = financeClaimService.majorStatExport(majorStat,keyword);
        exportPlus(response,"大中型实收款列表",list,RealFundMajorStatVO.class);
    }

    /**
     * 分页查询资产实收款统计
     * @param page 分页信息
     * @param dto 查询dto
     * @return Page<RealFundAssetsStatVO>
     */
    @GetMapping("assets/stat")
    public RestResponse<Page<RealFundAssetsStatVO>> assetsSate(Page page, RealFundAssetsStatDTO dto,String keyword) {
        return RestResponse.data(financeClaimService.assetsSate(page, dto,keyword));
    }

    /**
     * 资产实收款统计导出
     * @param dto 查询dto
     */
    @GetMapping("assets/stat/export")
    public void assetsSateExport(HttpServletResponse response, RealFundAssetsStatDTO dto,String keyword) throws IOException {
        List<RealFundAssetsStatVO> list = financeClaimService.assetsSateExport(dto,keyword);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (ObjectUtil.isNull(dto.getStartTime()) || ObjectUtil.isNull(dto.getEndTime())){
            ErrorCode.throwBusinessException("起始时间和结束时间必填！", "500");
        }if (ObjectUtil.isNull(list) || list.isEmpty()){
            ErrorCode.throwBusinessException("条件筛选结果数量为0,不可导出！", "500");
        }else {
            exportPlus(response, "资产实收款统计_" + dateFormat.format(new Date()),list, RealFundAssetsStatVO.class);
        }
    }

    /**
     * 个贷实收款统计列表
     */
    @GetMapping("/personal/stat")
    public RestResponse<Page<RealFundPersonalVO>> personalStat(RealFundPersonalVO personalVO, Page page){
        Page<RealFundPersonalVO> pages=financeClaimService.personalStat(page,personalVO);
        return RestResponse.data(pages);
    }

    /**
     * 个贷实收款统计列表导出
     */
    @GetMapping("/personal/stat/export")
    public void personalStatExport(RealFundPersonalVO personalVO, HttpServletResponse response) throws IOException {
        List<RealFundPersonalVO> list = financeClaimService.personalStatExport(personalVO);
        exportPlus(response, "个贷实收款统计", list, RealFundPersonalVO.class);
    }

    /**
     * 认领记录分页
     * @param financeClaimVO
     * @param page
     * @return
     */
    @DataPermission(ignoreDepartment = {MANAGER_OFFICE,FINANCE_DEPARTMENT})
    @GetMapping("/page")
    public RestResponse<Page<FinanceClaimsVO>> financeClaimsList(FinanceClaimsVO financeClaimVO, Page page,String keyword){
        Page<FinanceClaimsVO> ret = financeClaimService.financeClaimsList(page,financeClaimVO,keyword);
        return RestResponse.data(ret);
    }

    /**
     * 认领记录分页(只查询登陆人的)
     * @param financeClaimVO
     * @param page
     * @return
     */
    @GetMapping("/page/self")
    public RestResponse<Page<FinanceClaimsVO>> financeClaimsListSelf(FinanceClaimsVO financeClaimVO, Page page,String keyword){
        financeClaimVO.setClaimUserId(LoginContext.getUserId());
        Page<FinanceClaimsVO> ret = financeClaimService.financeClaimsList(page,financeClaimVO,keyword);
        return RestResponse.data(ret);
    }

    /**
     * 认领记录导出
     * @param financeClaimVO
     * @param keyword
     * @param response
     * @throws IOException
     */
    @GetMapping("/export")
    public void financeClaimsListExport(FinanceClaimsVO financeClaimVO,String keyword,HttpServletResponse response) throws IOException {
        List<FinanceClaimsVO> data = financeClaimService.exportList(financeClaimVO,keyword);
        exportPlus(response,"认领记录导出",data, FinanceClaimsVO.class);
    }
}

