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

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.contract.ContractTemplateBean;
import com.gree.mall.manager.bean.contract.ContractTemplateVO;
import com.gree.mall.manager.bean.contract.ContractWebsitBean;
import com.gree.mall.manager.enums.material.StateEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.contract.ContractTemplateLogic;
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.util.Map;


@Slf4j
@RestController
@Api(value = "合同模板API", tags ={"合同模板API"} )
@RequestMapping("/contract/template")
public class ContractTemplateController {

    @Resource
    ContractTemplateLogic contractTemplateLogic;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "合同模板-列表")
    public ResponseHelper<IPage<ContractTemplateVO>> list(@RequestBody ZfireParamBean zfireParamBean) {
        IPage<ContractTemplateVO> list = contractTemplateLogic.list(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<ContractTemplateVO>() {
        });
    }

    @PostMapping("/list/export")
    @ApiOperation(value = "合同模板-导出")
    public void listExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //2.查询要导出的内容
        IPage<ContractTemplateVO> list = contractTemplateLogic.list(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @PostMapping("/query/rela/websit")
    @ApiOperation(value = "查询上下级关系配置网点")
    public ResponseHelper<IPage<ContractWebsitBean>> queryRelaWebsit(
            @ApiParam(value = "合同模板id") @RequestParam(required = false) String contractTemplateId,
            @ApiParam(value = "网点编号", required = true) @RequestParam String websitId,
            @ApiParam(value = "网点名称", required = true) @RequestParam String websitName,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) throws RemoteServiceException {
        IPage<ContractWebsitBean> list = contractTemplateLogic.queryRelaWebsit(contractTemplateId, websitId, websitName, pageNum, pageSize);
        return ResponseHelper.success(list);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "合同模板-详情")
    public ResponseHelper<ContractTemplateBean> detail(
            @ApiParam(value = "合同模板id", required = true) @RequestParam String contractTemplateId
    ) {
        ContractTemplateBean detail = contractTemplateLogic.detail(contractTemplateId);
        return ResponseHelper.success(detail);
    }

    @PostMapping("add")
    @ApiOperation(value = "合同模板-添加")
    public ResponseHelper add(@RequestBody ContractTemplateBean contractTemplate) {
        contractTemplateLogic.add(contractTemplate);
        return ResponseHelper.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "合同模板-修改")
    public ResponseHelper update(@RequestBody ContractTemplateBean contractTemplate) {
        contractTemplateLogic.update(contractTemplate);
        return ResponseHelper.success();
    }

    @PostMapping("status")
    @ApiOperation(value = "合同模板-有效/无效")
    public ResponseHelper status(
            @ApiParam(value = "合同模板id", required = true) @RequestParam String contractTemplateId,
            @ApiParam(value = "ON=有效 OFF=无效", required = true) @RequestParam StateEnum status) {
        contractTemplateLogic.updateStatus(contractTemplateId, status);
        return ResponseHelper.success();
    }

    @PostMapping("del")
    @ApiOperation(value = "合同模板-删除")
    public ResponseHelper delete(@RequestParam String contractTemplateId) {
        contractTemplateLogic.delete(contractTemplateId);
        return ResponseHelper.success();
    }

    @PostMapping("/get/field")
    @ApiOperation(value = "获取模板字段")
    public ResponseHelper<Map<String, Object>> getField() {
        Map<String, Object> result = contractTemplateLogic.getField();
        return ResponseHelper.success(result);
    }
}
