package com.gree.mall.manager.controller.workerorder;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.ExcelData;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.websit.SettlementOrderDetail;
import com.gree.mall.manager.bean.websit.SettlementOrderManualDetail;
import com.gree.mall.manager.bean.workorder.*;
import com.gree.mall.manager.constant.Constant;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.logic.workorder.IncreConfigLogic;
import com.gree.mall.manager.logic.workorder.ManualOrderLogic;
import com.gree.mall.manager.logic.workorder.SettleOrderLogic;
import com.gree.mall.manager.plus.entity.SettlementOrderManual;
import com.gree.mall.manager.plus.entity.WorkerSettlementManual;
import com.gree.mall.manager.utils.excel.ExcelUtils;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
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.beans.factory.annotation.Autowired;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@RequiredArgsConstructor
@Slf4j
@RestController
@Api(value = "手工费", tags = {"手工费API"})
@RequestMapping(value = "/manual/order", produces = "application/json; charset=utf-8")
public class ManualOrderController {

    @Autowired
    private ManualOrderLogic manualOrderLogic;


    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "手工费汇总")
    public ResponseHelper<IPage<WorkerSettlementManualVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<WorkerSettlementManualVO> settlementManualIPage = manualOrderLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(settlementManualIPage, new TypeReference<WorkerSettlementManualVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation(value = "手工费汇总导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<WorkerSettlementManualVO> settlementManualIPage = manualOrderLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(settlementManualIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @ZfireList
    @PostMapping("/listSettlement")
    @ApiOperation(value = "手工费明细")
    public ResponseHelper<IPage<SettlementOrderManualVO>> listSettlement(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<SettlementOrderManualVO> settlementManualIPage = manualOrderLogic.listSettlement(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(settlementManualIPage, new TypeReference<SettlementOrderManualVO>() {});
    }

    @PostMapping("/listSettlement/export")
    @ApiOperation(value = "手工费明细导出")
    public void listSettlementExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<SettlementOrderManualVO> settlementManualIPage = manualOrderLogic.listSettlement(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(settlementManualIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }

    @PostMapping("/detail")
    @ApiOperation("结算详情")
    public ResponseHelper<SettlementOrderManualDetail> detail(
            @ApiParam(value = "结算id" , required = true) @RequestParam(required = true) String id,

            HttpServletRequest request
    ) throws ParseException {
        SettlementOrderManualDetail  settlementOrderDetail = manualOrderLogic.detail(id);
        return ResponseHelper.success(settlementOrderDetail);
    }

    @PostMapping("/settlement")
    @ApiOperation("结算")
    public ResponseHelper settlement(
            @ApiParam(value = "结算id" , required = true) @RequestParam(required = true) List<String> id,
            HttpServletRequest request
    ) throws ParseException {
        manualOrderLogic.settlement(id);
        return ResponseHelper.success();
    }



}
