package cn.ey.biz.module.system.controller.engagement.code;

import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.common.pojo.R;
import cn.ey.biz.module.system.controller.engagement.code.vo.*;
import cn.ey.biz.module.system.controller.engagement.task.vo.EngagementTaskRuleInfoReqVO;
import cn.ey.biz.module.system.convert.engagement.code.EngagementCodeRuleConvert;
import cn.ey.biz.module.system.dal.dataobject.codeblock.CodeBlockLegalEntityDO;
import cn.ey.biz.module.system.dal.dataobject.engagement.code.SystemEngagementCodeRuleDO;
import cn.ey.biz.module.system.service.engagement.code.EngagementCodeRuleService;
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 java.util.List;

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

@Api(tags = "项目管理 - Code规则生成")
@RestController
@RequestMapping("/system/engagement/code/rule")
public class EngagementCodeRuleController {

    @Resource
    private EngagementCodeRuleService codeRuleService;

    @PostMapping("/create")
    @ApiOperation("创建Code规则")
    @PreAuthorize("@ss.hasPermission('system:engage-code:edit')")
    public R<List<Long>> createCodeRule(@RequestBody List<EngagementCodeRuleReq> reqVO) {
        List<Long> taskRuleId = codeRuleService.createCodeRule(reqVO);
        return success(taskRuleId);
    }


    @PostMapping("/delete")
    @ApiOperation("删除Code规则")
    @PreAuthorize("@ss.hasPermission('system:engage-code:edit')")
    public R<Boolean> deleteCodeRule(
            @Validated(value = EngagementCodeRuleEntityVO.Deleted.class)
            @RequestBody EngagementTaskRuleInfoReqVO reqVO) {
        codeRuleService.deleteCodeRule(reqVO.getIds());
        return success(true);
    }

    @PostMapping("/get")
    @ApiOperation("查询任务规则详细")
    @PreAuthorize("@ss.hasPermission('system:engage-code:edit')")
    public R<EngagementCodeRuleRespVO> getCodeRule(
            @Validated(value = EngagementCodeRuleEntityVO.QueryById.class)
            @RequestBody EngagementTaskRuleInfoReqVO reqVO) {
        EngagementCodeRuleRespVO taskRule = codeRuleService.getCodeRule(reqVO.getId());
        return success(taskRule);
    }

    @PostMapping("/page")
    @ApiOperation("Code规则分页列表")
    @PreAuthorize("@ss.hasPermission('system:engage-code:query')")
    public R<PageResult<EngagementCodeRuleRespVO>> getCodeRulePage(@RequestBody EngagementCodeRuleEntityVO reqVO) {
        PageResult<EngagementCodeRuleRespVO> taskRule = codeRuleService.getCodeRulePage(reqVO);
        List<EngagementCodeRuleRespVO> list = taskRule.getList();
        for (EngagementCodeRuleRespVO entityDO : list) {
            if(StringUtils.isBlank(entityDO.getSlCode())){
                entityDO.setSlCode("-");
            }
            if(StringUtils.isBlank(entityDO.getSslCode())){
                entityDO.setSslCode("-");
            }
        }
        taskRule.setList(list);
        return success(taskRule);
    }

    @PostMapping("/list-all-simple")
    @ApiOperation("获得全部Code规则列表")
    public R<List<EngagementCodeRuleSimpleRespVO>> getCodeRuleSimpleList() {
        List<SystemEngagementCodeRuleDO> codeRuleSimpleList = codeRuleService.getCodeRuleSimpleList();
        List<EngagementCodeRuleSimpleRespVO> doList = EngagementCodeRuleConvert.INSTANCE.doConvertSimpleVo(codeRuleSimpleList);
        return success(doList);
    }


    @GetMapping("/operation/record")
    @ApiOperation("最后一次更新人")
    @PreAuthorize("@ss.hasPermission('system:engage-code:query')")
    public R<EngagementCodeRuleRecordRespVO> getOperationRecord() {
        EngagementCodeRuleRecordRespVO operationRecord = codeRuleService.getOperationRecord();
        return success(operationRecord);
    }
}
