package cn.ey.biz.module.system.controller.admin.codeblock;

import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.common.pojo.R;
import cn.ey.biz.module.system.api.codeblock.dto.*;
import cn.ey.biz.module.system.controller.admin.codeblock.vo.*;
import cn.ey.biz.module.system.convert.codeblock.CodeBlockLegalEntityConvert;
import cn.ey.biz.module.system.dal.dataobject.codeblock.CodeBlockLegalEntityDO;
import cn.ey.biz.module.system.service.codeblock.CodeBlockLegalEntityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

import static cn.ey.biz.common.pojo.R.success;

@Api(tags = "管理后台 - 法律实体")
@RestController
@RequestMapping("/system/code/block/legal/entity")
@Validated
public class CodeBlockLegalEntityController {
    @Resource
    private CodeBlockLegalEntityService codeBlockLegalEntityService;

    @PostMapping("/create")
    @ApiOperation("创建法律实体")
    @PreAuthorize("@ss.hasPermission('system:engage-code:edit')")
    public R<List<Long>> createTaskRule(
            @Validated(value = CodeBlockLegalEntityCreateReqVO.Save.class)
            @RequestBody CodeBlockLegalEntityCreateReqVO reqVO) {
        List<Long> taskRuleId = codeBlockLegalEntityService.createLegalEntity(reqVO);
        return success(taskRuleId);
    }

    @PostMapping("/delete")
    @ApiOperation("删除法律实体")
    @PreAuthorize("@ss.hasPermission('system:engage-code:edit')")
    public R<Boolean> deleteLegalEntity(
            @Validated(value = CodeBlockLegalEntityCreateReqVO.Deleted.class)
            @RequestBody CodeBlockLegalEntityCreateReqVO reqVO) {
        codeBlockLegalEntityService.deleteLegalEntity(reqVO.getIds());
        return success(true);
    }

    @PostMapping("/get")
    @ApiOperation("查询法律实体详细")
    @PreAuthorize("@ss.hasPermission('system:engage-code:query')")
    public R<CodeBlockLegalEntityDetailRespVO> getLegalEntity(
            @Validated(value = CodeBlockLegalEntityCreateReqVO.QueryById.class)
            @RequestBody CodeBlockLegalEntityCreateReqVO reqVO) {
        CodeBlockLegalEntityDetailRespVO respVO = codeBlockLegalEntityService.getLegalEntity(reqVO.getId());
        return success(respVO);
    }


    @PostMapping("/page")
    @ApiOperation("法律实体分页查询")
    @PreAuthorize("@ss.hasPermission('system:engage-code:query')")
    public R<PageResult<CodeBlockLegalEntityPageRespVO>> listPage(@RequestBody CodeBlockLegalEntityPageReqVO codeBlockPageReqVO) {
        PageResult<CodeBlockLegalEntityDO> dictDataPage = codeBlockLegalEntityService.getDictDataPage(codeBlockPageReqVO);
        List<CodeBlockLegalEntityDO> list = dictDataPage.getList();
        for (CodeBlockLegalEntityDO entityDO : list) {
            if (StringUtils.isBlank(entityDO.getSlCode())) {
                entityDO.setSlCode("-");
            }
            if (StringUtils.isBlank(entityDO.getSslCode())) {
                entityDO.setSslCode("-");
            }
        }
        dictDataPage.setList(list);
        return success(CodeBlockLegalEntityConvert.INSTANCE.convertPage(dictDataPage));
    }


    @GetMapping("/pull/down")
    @ApiOperation("法律实体下拉（Legal Entity）")
    @PreAuthorize("@ss.hasPermission('system:engage-code:query')")
    public R<List<String>> pullDown() {
        List<String> legalEntity = codeBlockLegalEntityService.pullDown();
        return success(legalEntity);
    }

    @GetMapping("/pull/down/sl")
    @ApiOperation("法律实体下拉（sl）")
    @PreAuthorize("@ss.hasPermission('system:engage-code:query')")
    public R<List<CodeBlockCodeValueVO>> pullDownSl(
            @NotBlank(message = "法律实体编码不能为空")
            @RequestParam("legalEntityCode") String legalEntityCode) {
        List<CodeBlockCodeValueVO> legalEntity = codeBlockLegalEntityService.pullDownSl(legalEntityCode);
        return success(legalEntity);
    }

    @PostMapping("/pull/down/slList")
    @ApiOperation("法律实体下拉（slList）")
    @PreAuthorize("@ss.hasPermission('system:engage-code:query')")
    public R<List<CodeBlockCodeValueVO>> pullDownSlList(
            @NotNull(message = "法律实体编码不能为空")
            @RequestBody List<String> legalEntityCode) {
        List<CodeBlockCodeValueVO> legalEntity = codeBlockLegalEntityService.pullDownSlList(legalEntityCode);
        return success(legalEntity);
    }

    @PostMapping("/pull/down/sSlList")
    @ApiOperation("法律实体下拉（sSlList）")
    @PreAuthorize("@ss.hasPermission('system:engage-code:query')")
    public R<List<CodeBlockCodeValueVO>> pullDownSSlList(
            @Validated @RequestBody SslCodePullDownListVO downListVO) {
        List<CodeBlockCodeValueVO> legalEntity = codeBlockLegalEntityService.pullDownSslCodeList(downListVO.getLegalEntityCode(), downListVO.getSlCode());
        return success(legalEntity);
    }

    @GetMapping("/pull/down/smu")
    @ApiOperation("法律实体下拉（smu）")
    @PreAuthorize("@ss.hasPermission('system:engage-code:query')")
    public R<List<CodeBlockCodeValueVO>> pullDownSmu(
            @NotBlank(message = "法律实体编码不能为空")
            @RequestParam("legalEntityCode") String legalEntityCode,
            @RequestParam("slCode") String slCode, @RequestParam("sslCode") String sslCode) {
        List<CodeBlockCodeValueVO> legalEntity = codeBlockLegalEntityService.pullDownSmu(legalEntityCode, slCode, sslCode);
        return success(legalEntity);
    }

    @PostMapping("/pull/down/smuList")
    @ApiOperation("法律实体下拉（smuList）")
    @PreAuthorize("@ss.hasPermission('system:engage-code:query')")
    public R<List<CodeBlockCodeValueVO>> pullDownSmuList(
            @Validated @RequestBody SmuPullDownListVO downListVO) {
        List<CodeBlockCodeValueVO> legalEntity = codeBlockLegalEntityService.pullDownSmuList(downListVO.getLegalEntityCode(), downListVO.getSlCode(), downListVO.getSslCode());
        return success(legalEntity);
    }


    @PostMapping("/filterList")
    @ApiOperation("根据code规则与法律实体过滤宽表数据")
    public R<CodeBlockWideTableDTO> filterList(
            @RequestBody CodeBlockWideTableReqDTO reqDTO) {
        CodeBlockWideTableDTO codeBlockWideTableDTO = codeBlockLegalEntityService.filterList(reqDTO);
        return success(codeBlockWideTableDTO);
    }

    @PostMapping("/getLegalEffective")
    @ApiOperation("获取合法的codeBlock")
    public R<CodeBlockLegalRespDTO> getLegalEffective(
            @RequestBody CodeBlockReqDTO reqDTO) {
        CodeBlockLegalRespDTO legalRespDTO = codeBlockLegalEntityService.getLegalEffective(reqDTO);
        return success(legalRespDTO);
    }


    @GetMapping("/getCodeBlockInfo")
    @ApiOperation("获取code bloc")
    public R<CodeBlockInfoRespDTO> getCodeBlockInfo(
            @RequestParam("codeBlock") String codeBlock) {
        CodeBlockInfoRespDTO codeBlockInfo = codeBlockLegalEntityService.getCodeBlockInfo(codeBlock);
        return success(codeBlockInfo);
    }


    @PostMapping("/finance/filterList")
    @ApiOperation("根据code规则与法律实体过滤宽表数据")
    public R<CodeBlockWideTableDTO> financeFilterList(
            @RequestBody CodeBlockWideTableReqDTO reqDTO) {
        CodeBlockWideTableDTO codeBlockWideTableDTO = codeBlockLegalEntityService.financeFilterList(reqDTO);
        return success(codeBlockWideTableDTO);
    }

    @PostMapping("/finance/code-block-list")
    @ApiOperation("根据code规则与法律实体过滤宽表数据")
    public R<CodeBlockWideTableDTO> getCodeBlockList(
            @RequestBody CodeBlockWideTableReqDTO reqDTO) {
        CodeBlockWideTableDTO codeBlockWideTableDTO = codeBlockLegalEntityService.getCodeBlockList(reqDTO);
        return success(codeBlockWideTableDTO);
    }

    @PostMapping("/finance/firm/filterList")
    @ApiOperation("根据code规则与法律实体过滤宽表数据")
    public R<CodeBlockWideTableDTO> financeFilterFirmList(
            @RequestBody CodeBlockWideTableReqDTO reqDTO) {
        CodeBlockWideTableDTO codeBlockWideTableDTO = codeBlockLegalEntityService.financeFilterFirmList(reqDTO);
        return success(codeBlockWideTableDTO);
    }
}
