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.workorder.InstallSettleNormVO;
import com.gree.mall.manager.bean.workorder.OtherSettleNormVO;
import com.gree.mall.manager.bean.workorder.RepairSettleNormVO;
import com.gree.mall.manager.enums.workorder.SettleNormTypeEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.workorder.SettleNormLogic;
import com.gree.mall.manager.plus.entity.PgSettleNorm;
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.web.bind.annotation.*;

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

@RequiredArgsConstructor
@Slf4j
@RestController
@Api(value = "结算费用标准API", tags = {"结算费用标准API"})
@RequestMapping(value = "/settle/norm/", produces = "application/json; charset=utf-8")
public class SettleNormController {

    private final SettleNormLogic settleNormLogic;

    @ZfireList
    @PostMapping("/install/list")
    @ApiOperation(value = "结算标准安装-列表")
    public ResponseHelper<Page<InstallSettleNormVO>> installList(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<InstallSettleNormVO> orderSmallTypeVO = settleNormLogic.installList(zfireParamBean);
        return ResponseHelper.success(orderSmallTypeVO, new TypeReference<InstallSettleNormVO>() {});
    }

    @PostMapping("/install/list/export")
    @ApiOperation(value = "结算标准安装-导出")
    public void installListExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //2.查询要导出的内容
        IPage<InstallSettleNormVO> page = settleNormLogic.installList(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @ZfireList
    @PostMapping("/repair/list")
    @ApiOperation(value = "结算标准维修-列表")
    public ResponseHelper<Page<RepairSettleNormVO>> repairList(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<RepairSettleNormVO> orderSmallTypeVO = settleNormLogic.repairList(zfireParamBean);
        return ResponseHelper.success(orderSmallTypeVO, new TypeReference<RepairSettleNormVO>() {});
    }

    @PostMapping("/repair/list/export")
    @ApiOperation(value = "结算标准维修-导出")
    public void repairListExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //2.查询要导出的内容
        IPage<RepairSettleNormVO> page = settleNormLogic.repairList(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @ZfireList
    @PostMapping("/other/list")
    @ApiOperation(value = "结算标准其它-列表")
    public ResponseHelper<Page<OtherSettleNormVO>> otherList(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<OtherSettleNormVO> orderSmallTypeVO = settleNormLogic.otherList(zfireParamBean);
        return ResponseHelper.success(orderSmallTypeVO, new TypeReference<OtherSettleNormVO>() {});
    }

    @PostMapping("/other/list/export")
    @ApiOperation(value = "结算标准安装-导出")
    public void otherListExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //2.查询要导出的内容
        IPage<OtherSettleNormVO> page = settleNormLogic.otherList(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "结算标准-详情")
    public ResponseHelper<PgSettleNorm> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) throws RemoteServiceException {
        return ResponseHelper.success(settleNormLogic.detail(id));
    }

    @PostMapping("/install/add")
    @ApiOperation(value = "结算标准安装-添加")
    public ResponseHelper<PgSettleNorm> installAdd(
            @RequestBody PgSettleNorm norm
    ) throws Exception {
        norm.setSettleNormType(SettleNormTypeEnum.INSTALL.getKey());
        settleNormLogic.add(norm);

        return ResponseHelper.success();
    }

    @PostMapping("/repair/add")
    @ApiOperation(value = "结算标准维修-添加")
    public ResponseHelper<PgSettleNorm> repairAdd(
            @RequestBody PgSettleNorm norm
    ) throws Exception {
        norm.setSettleNormType(SettleNormTypeEnum.REPAIR.getKey());
        settleNormLogic.add(norm);

        return ResponseHelper.success();
    }

    @PostMapping("/other/add")
    @ApiOperation(value = "结算标准其它-添加")
    public ResponseHelper<PgSettleNorm> otherAdd(
            @RequestBody PgSettleNorm norm
    ) throws Exception {
        norm.setSettleNormType(SettleNormTypeEnum.OTHER.getKey());
        settleNormLogic.add(norm);

        return ResponseHelper.success();
    }

    @PostMapping("/install/edit")
    @ApiOperation(value = "结算标准安装-编辑")
    public ResponseHelper<PgSettleNorm> installEdit(
            @RequestBody PgSettleNorm norm
    ) throws Exception {
        norm.setSettleNormType(SettleNormTypeEnum.INSTALL.getKey());
        settleNormLogic.edit(norm);

        return ResponseHelper.success();
    }

    @PostMapping("/repair/edit")
    @ApiOperation(value = "结算标准维修-编辑")
    public ResponseHelper<PgSettleNorm> repairEdit(
            @RequestBody PgSettleNorm norm
    ) throws Exception {
        norm.setSettleNormType(SettleNormTypeEnum.REPAIR.getKey());
        settleNormLogic.edit(norm);

        return ResponseHelper.success();
    }

    @PostMapping("/other/edit")
    @ApiOperation(value = "结算标准其它-编辑")
    public ResponseHelper<PgSettleNorm> otherEdit(
            @RequestBody PgSettleNorm norm
    ) throws Exception {
        norm.setSettleNormType(SettleNormTypeEnum.OTHER.getKey());
        settleNormLogic.edit(norm);

        return ResponseHelper.success();
    }

    @PostMapping("/delete")
    @ApiOperation("结算标准-删除")
    public ResponseHelper delete(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) {
        settleNormLogic.delete(id);
        return ResponseHelper.success();
    }

    @PostMapping("/install/query")
    @ApiOperation(value = "结算标准安装-查询")
    public ResponseHelper<List<PgSettleNorm>> installQuery(
            @ApiParam(value = "类型 INSTALL=安装 REPAIR=维修") @RequestParam(required = false) String settleNormType,
            @ApiParam(value = "类型") @RequestParam(required = false) String type,
            @ApiParam(value = "产品大类") @RequestParam(required = false) String mainId,
            @ApiParam(value = "产品小类") @RequestParam(required = false) String smallId,
            @ApiParam(value = "功率（质保类型 INSIDE=保内 OUT=保外）") @RequestParam(required = false) String label
    ) throws Exception {
        List<PgSettleNorm> list = settleNormLogic.installQuery(settleNormType, type, mainId, smallId, label);

        return ResponseHelper.success(list);
    }

    @PostMapping("/other/query")
    @ApiOperation(value = "结算标准其它-查询")
    public ResponseHelper<List<PgSettleNorm>> otherQuery(
            @ApiParam(value = "类型") @RequestParam(required = false) String type,
            @ApiParam(value = "规格") @RequestParam(required = false) String label
    ) throws Exception {
        List<PgSettleNorm> list = settleNormLogic.otherQuery(type, label);

        return ResponseHelper.success(list);
    }
}
