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

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.engin.*;
import com.gree.mall.manager.bean.websit.WorkerSettlementVO;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.engin.ProjectRepairLogic;
import com.gree.mall.manager.logic.engin.RpMaterialLogic;
import com.gree.mall.manager.plus.entity.RpMaterialOrder;
import com.gree.mall.manager.plus.entity.RpProjectRepairBy;
import com.gree.mall.manager.plus.entity.RpProjectRepairProduct;
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.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.List;

@Slf4j
@RestController
@Api(value = "工程维保费用记录", tags = {"工程维保费用记录"})
@RequestMapping(value = "/engin/material", produces = "application/json; charset=utf-8")
public class RpMaterialController {

    @Resource
    RpMaterialLogic rpMaterialLogic;

    @ZfireList
    @PostMapping("/list/pageSettlementV2")
    @ApiOperation(value = "工程维保线下汇总列表")
    public ResponseHelper<IPage<WorkerMaterialSettlementVO>> pageSettlementV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WorkerMaterialSettlementVO> page = rpMaterialLogic.workerSettlementPageV2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<WorkerMaterialSettlementVO>() {});
    }

    @PostMapping("/pageSettlementExport")
    @ApiOperation(value = "工程维保线下汇总列表导出")
    public void pageSettlementExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<WorkerMaterialSettlementVO> page = rpMaterialLogic.workerSettlementPageV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }




    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "工程维保费用记录-列表")
    public ResponseHelper<Page<RpMaterialOrderVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<RpMaterialOrderVO> vo = rpMaterialLogic.list(zfireParamBean);
        return ResponseHelper.success(vo, new TypeReference<RpMaterialOrderVO>() {});
    }

    @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<RpMaterialOrderVO> page = rpMaterialLogic.list(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }

    @ZfireList
    @PostMapping("/listAll")
    @ApiOperation(value = "工程维保费用记录全包-列表")
    public ResponseHelper<Page<RpMaterialOrderAllListVO>> listAll(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<RpMaterialOrderAllListVO> vo = rpMaterialLogic.listAll(zfireParamBean);
        return ResponseHelper.success(vo, new TypeReference<RpMaterialOrderVO>() {});
    }

    @PostMapping("/listAll/export")
    @ApiOperation(value = "工程维保费用记录全包-导出")
    public void listAllExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<RpMaterialOrderAllListVO> page = rpMaterialLogic.listAll(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }

    @ZfireList
    @PostMapping("/listWechat")
    @ApiOperation(value = "工程维保费用记录-微信列表")
    public ResponseHelper<Page<RpMaterialOrderWechatVO>> listWechat(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<RpMaterialOrderWechatVO> vo = rpMaterialLogic.listWechat(zfireParamBean);
        return ResponseHelper.success(vo, new TypeReference<RpMaterialOrderVO>() {});
    }

    @PostMapping("/listWechat/export")
    @ApiOperation(value = "工程维保费用记录微信列表-导出")
    public void listWechatExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<RpMaterialOrderWechatVO> page = rpMaterialLogic.listWechat(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }

    @ZfireList
    @PostMapping("/listLine")
    @ApiOperation(value = "工程维保费用记录线下-列表")
    public ResponseHelper<Page<RpMaterialOrderLineVO>> listLine(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<RpMaterialOrderLineVO> vo = rpMaterialLogic.listLine(zfireParamBean);
        return ResponseHelper.success(vo, new TypeReference<RpMaterialOrderVO>() {});
    }

    @PostMapping("/listLine/export")
    @ApiOperation(value = "工程维保费用记录线下-导出")
    public void listLineExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<RpMaterialOrderLineVO> page = rpMaterialLogic.listLine(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }



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

    @PostMapping("/lineSettlementHui")
    @ApiOperation("汇总批量线下结算")
    public ResponseHelper lineSettlementHui(
            @ApiParam(value = "workerId" , required = true) @RequestParam(required = true) String workerId,
            @ApiParam(value = "workerId" , required = true) @RequestParam(required = true) String websitId,
            HttpServletRequest request
    ) throws ParseException {
        rpMaterialLogic.lineSettlementHui(workerId,websitId);
        return ResponseHelper.success();
    }




    @PostMapping("/detail")
    @ApiOperation(value = "工程维保费用记录-详情")
    public ResponseHelper<RpmaterialOrderDetail> detail(
            @ApiParam(value = "申请单号",required = true) @RequestParam(required = true) String id

    ) throws Exception {

        RpmaterialOrderDetail rpmaterialOrderDetail = rpMaterialLogic.detail(id);

        return ResponseHelper.success(rpmaterialOrderDetail);
    }

    @PostMapping("/examine")
    @ApiOperation(value = "工程维保费用记录-审批")
    public ResponseHelper examine(
            @ApiParam(value = "申请单号",required = true) @RequestParam(required = true) String id,
            @ApiParam(value = "审批状态 WAIT 待审 OK 审批  FAIL 驳回  PAID 现场支付 NO 取消 NO_PAID 未支付",required = true) @RequestParam(required = true) String examineStatus,
            @ApiParam(value = "审批备注",required = false) @RequestParam(required = false) String examineRemark

            ) throws Exception {

        rpMaterialLogic.examine(id,examineRemark,examineStatus);

        return ResponseHelper.success();
    }


    @PostMapping("/isBy")
    @ApiOperation(value = "用工程维保费用记录-是否可以审批")
    public ResponseHelper<Boolean> isBy(

            @ApiParam(value = "工单号",required = true) @RequestParam(required = true) String orderBaserId

    ) throws Exception {

        Boolean isBy = rpMaterialLogic.isBy(orderBaserId);

        return ResponseHelper.success(isBy);
    }

    @PostMapping("/cancel")
    @ApiOperation(value = "工程维保费用记录-取消")
    public ResponseHelper cancel(
            @ApiParam(value = "申请单号",required = true) @RequestParam(required = true) String id

    ) throws Exception {

        rpMaterialLogic.cancel(id);

        return ResponseHelper.success();
    }

    @PostMapping("/detailWorker")
    @ApiOperation(value = "工程维保费用记录详情-电子支付列表")
    public ResponseHelper<List<RpMaterialOrderAllVO>> detailWorker(
            @ApiParam(value = "工单号", required = true)   @RequestParam(required = true) String workerOrderId


    ) throws RemoteServiceException {
        List<RpMaterialOrderAllVO> rpMaterialOrder = rpMaterialLogic.detailWorker(workerOrderId);
        return ResponseHelper.success(rpMaterialOrder);
    }



}
