package com.gree.mall.manager.controller.settle.repair;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.bean.ExcelData;
import com.gree.mall.manager.bean.settle.repair.DailyNotMonthSummaryCountBean;
import com.gree.mall.manager.bean.settle.repair.DailyWorkerIssueSalaryBean;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.settle.repair.DailyImportSummaryLogic;
import com.gree.mall.manager.logic.settle.repair.DailyMonthSummaryLogic;
import com.gree.mall.manager.plus.entity.SettleDailyIssueSummaryMonthRecord;
import com.gree.mall.manager.plus.entity.SettleDailyMonthlySummary;
import com.gree.mall.manager.utils.excel.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@RestController
@Api(value = "月结算API", tags = {"月结算API"})
@RequestMapping(value = "/daily/summary", produces = "application/json; charset=utf-8")
@RequiredArgsConstructor
public class DailyMonthSummaryController {

    private final DailyMonthSummaryLogic dailyMonthSummaryLogic;
    private final DailyImportSummaryLogic dailyImportSummaryLogic;


    @GetMapping("/month/salary/export")
    @ApiOperation("月度师傅汇总-导出")
    public void monthSalaryExport(
            @ApiParam(required = true, value = "汇总批次号") @RequestParam String summaryBatchNo,
            @ApiParam(required = true, value = "结算单位") @RequestParam String summaryNumber,
            @ApiParam(value = "状态 1.待发放 2.已发放3.银行受理中") @RequestParam(required = false) Integer status,
            @ApiParam(value = "所属网点") @RequestParam(required = false) String websiteNumber,
            @ApiParam(value = "所属网点名称") @RequestParam(required = false) String websiteName,
            @ApiParam(value = "服务人员") @RequestParam(required = false) String serviceName,
            @ApiParam(value = "身份证号码") @RequestParam(required = false) String idCard,
            @ApiParam(value = "手机号") @RequestParam(required = false) String mobile,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        IPage<SettleDailyIssueSummaryMonthRecord> page = dailyImportSummaryLogic.workerSummaryMonthList(summaryBatchNo, summaryNumber,
                status,websiteNumber,serviceName,idCard,mobile,websiteName,1, -1);
        ExcelData excelData = dailyMonthSummaryLogic.monthSalaryExport(page.getRecords());
        ExcelUtils.exportExcel(request, response, "师傅工资月度汇总.csv", excelData);
    }

    @ApiOperation("月度师傅汇总-导入")
    @PostMapping("/month/salary/import")
    public ResponseHelper monthSalaryImport(
            @ApiParam(required = true, value = "附件") @RequestPart("file") MultipartFile file
    ) throws Exception {
        dailyMonthSummaryLogic.monthSalaryImport(file);
        return ResponseHelper.success();
    }


    @GetMapping("/worker/month/list")
    @ApiOperation(value = "师傅汇总-月结")
    public ResponseHelper<IPage<SettleDailyIssueSummaryMonthRecord>> workerSummaryMonthList(
            @ApiParam(required = true, value = "页号") @RequestParam Integer pageNo,
            @ApiParam(required = true, value = "页大小") @RequestParam Integer pageSize,
            @ApiParam(required = true, value = "汇总批次号") @RequestParam String summaryBatchNo,
            @ApiParam(required = true, value = "结算单位") @RequestParam String summaryNumber,
            @ApiParam(value = "状态 1.待发放 2.已发放3.银行受理中") @RequestParam(required = false) Integer status,
            @ApiParam(value = "所属网点") @RequestParam(required = false) String websiteNumber,
            @ApiParam(value = "所属网点名称") @RequestParam(required = false) String websiteName,
            @ApiParam(value = "服务人员") @RequestParam(required = false) String serviceName,
            @ApiParam(value = "身份证号码") @RequestParam(required = false) String idCard,
            @ApiParam(value = "手机号") @RequestParam(required = false) String mobile

    ) {
        IPage<SettleDailyIssueSummaryMonthRecord> page = dailyImportSummaryLogic.workerSummaryMonthList(summaryBatchNo, summaryNumber,
                status,websiteNumber,serviceName,idCard,mobile,websiteName,pageNo, pageSize);
        return ResponseHelper.success(page);
    }


    @GetMapping("/month/list")
    @ApiOperation(value = "月度汇总-列表")
    public ResponseHelper<IPage<DailyNotMonthSummaryCountBean>> monthlyList(
            @ApiParam(required = true, value = "页号") @RequestParam Integer pageNo,
            @ApiParam(required = true, value = "页大小") @RequestParam Integer pageSize,
            @ApiParam(value = "汇总批次号") @RequestParam(required = false) String summaryBatchNo,
            @ApiParam(value = "汇总时间段起始") @RequestParam(required = false) String summaryStartTime,
            @ApiParam(value = "汇总时间段结束") @RequestParam(required = false) String summaryEndTime,
            @ApiParam(value = "发放时间段起始") @RequestParam(required = false) String issueStartTime,
            @ApiParam(value = "发放时间段结束") @RequestParam(required = false) String issueEndTime,
            @ApiParam(value = "状态 1.未发放 2.已发放") @RequestParam(required = false) Integer status

    ) {
        IPage<DailyNotMonthSummaryCountBean> page = dailyImportSummaryLogic.cwSummaryMonthList(summaryBatchNo, summaryStartTime,
                summaryEndTime, issueStartTime, issueEndTime, status,pageNo, pageSize);
        return ResponseHelper.success(page);
    }


    @GetMapping("/month/website/list")
    @ApiOperation(value = "月度汇总结算网点-列表")
    public ResponseHelper<List<DailyNotMonthSummaryCountBean>> monthlyWebsiteList(
            @ApiParam(required = true, value = "汇总批次号") @RequestParam String summaryBatchNo
    ) {
        List<DailyNotMonthSummaryCountBean> page = dailyMonthSummaryLogic.monthlySummaryList(summaryBatchNo);
        return ResponseHelper.success(page);
    }

    @GetMapping("/month/detail/list")
    @ApiOperation(value = "月度结算师傅详情-列表")
    public ResponseHelper<List<DailyWorkerIssueSalaryBean>> monthlyDetailList(
            @ApiParam(required = true, value = "页号") @RequestParam Integer pageNo,
            @ApiParam(required = true, value = "页大小") @RequestParam Integer pageSize,
            @ApiParam(required = true, value = "月度汇总批次号") @RequestParam String summaryMonthBatchNo,
            @ApiParam(required = true, value = "结算网点") @RequestParam String summaryNumber,
            @ApiParam(required = true, value = "师傅编号") @RequestParam String serviceNumber
    ) {
        List<DailyWorkerIssueSalaryBean> page = dailyMonthSummaryLogic.monthlyDetailList(summaryMonthBatchNo,summaryNumber,
                serviceNumber,pageNo,pageSize);
        return ResponseHelper.success(page);
    }


    @PostMapping("/month/do")
    @ApiOperation(value = "月度汇总")
    public ResponseHelper<IPage<SettleDailyMonthlySummary>> monthlyDo(

    ) {
        dailyMonthSummaryLogic.monthlyDo();
        return ResponseHelper.success();
    }

    @PostMapping("/month/reDo")
    @ApiOperation(value = "重新汇总")
    public ResponseHelper<IPage<SettleDailyMonthlySummary>> monthlyDo(
            @ApiParam(required = true, value = "月度汇总批次号") @RequestParam String summaryMonthBatchNo
    ) {
        dailyMonthSummaryLogic.monthlyReDo(summaryMonthBatchNo);
        return ResponseHelper.success();
    }


    @ApiOperation("月度计算费用")
    @PostMapping("/month/salary/calculate")
    public ResponseHelper monthSalaryCalculate(
            @ApiParam(required = true, value = "月度汇总批次号") @RequestParam String summaryMonthBatchNo,
            @ApiParam(required = true, value = "结算单位") @RequestParam String summaryNumber
    ) {
        dailyMonthSummaryLogic.monthSalaryCalculate(summaryMonthBatchNo, summaryNumber);
        return ResponseHelper.success();
    }


    @ApiOperation("月度工资-发放")
    @PostMapping("/month/salary/issue")
    public ResponseHelper monthSalaryIssue(
            @ApiParam(required = true, value = "月度汇总批次号") @RequestParam String summaryMonthBatchNo,
            @ApiParam(value = "结算单位") @RequestParam(required = false) List<String> summaryNumberList
    ) {
        dailyMonthSummaryLogic.monthSalaryIssue(summaryMonthBatchNo,summaryNumberList);
        return ResponseHelper.success();
    }

    @GetMapping("/month/detail/export")
    @ApiOperation(value = "月度结算师傅详情-导出")
    public void monthlyDetailExport(
            @ApiParam(required = true, value = "月度汇总批次号") @RequestParam String summaryMonthBatchNo,
            @ApiParam(required = true, value = "结算网点") @RequestParam String summaryNumber,
            @ApiParam(required = true, value = "师傅编号") @RequestParam String serviceNumber,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        List<DailyWorkerIssueSalaryBean> page = dailyMonthSummaryLogic.monthlyDetailList(summaryMonthBatchNo,summaryNumber,
                serviceNumber,1,-1);
        ExcelData excelData = dailyImportSummaryLogic.workerDetailExport(page);
        ExcelUtils.exportExcel(request, response, "月结师傅汇总详情.xlsx", excelData);
    }
}
