package com.storlead.sales.controller.systemrule;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.storlead.framework.common.constant.CommonConstant;
import com.storlead.framework.common.result.Result;
import com.storlead.sales.pojo.dto.systemrule.SystemRuleModuleConfigDTO;
import com.storlead.sales.pojo.entity.systemrule.SystemRuleFieldConfigEntity;
import com.storlead.sales.pojo.entity.systemrule.SystemRuleModuleConfigEntity;
import com.storlead.sales.pojo.vo.systemrule.*;
import com.storlead.sales.service.systemrule.SystemRuleFieldConfigService;
import com.storlead.sales.service.systemrule.SystemRuleModuleConfigService;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 规则配置模块表 前端控制器
 * </p>
 *
 * @author YPZ
 * @since 2024-04-03
 */
@RestController
@RequestMapping("/system-rule-module-config")
@Api(tags = "销售: 系统配置相关接口")
public class SystemRuleModuleConfigController {

    @Resource
    private SystemRuleModuleConfigService systemRuleModuleConfigService;

    @Resource
    private SystemRuleFieldConfigService fieldConfigService;
    @PostMapping(value = "/list")
    @ApiOperation(value = "规则配置相关接口-列表", notes = "规则配置相关接口-列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SystemRuleModuleConfigDTO.class)
    })
    public Result<?> list(@ApiParam("规则配置请求参数类") SystemRuleModuleConfigDTO systemRuleModuleConfigDTO) {
        IPage<SystemRuleModuleConfigVO> pageList = systemRuleModuleConfigService.listByKeywords(systemRuleModuleConfigDTO);
        return Result.result(pageList);
    }

    @PostMapping(value = "/get-by-id")
    @ApiOperation(value = "规则配置相关接口-通过id查询规则配置", notes = "规则配置相关接口-通过id查询规则配置")
    public Result<?> getById(Long id,String resourceType) {

        return systemRuleModuleConfigService.getDetailById(id,resourceType);
    }

    @PostMapping(value = "/save")
    @ApiOperation(value = "规则配置相关接口-编辑", notes = "规则配置相关接口-编辑")
    @Transactional(rollbackFor = Throwable.class)
    public Result<?> save(@RequestBody SystemRuleModuleConfigDTO configDTO) {

        List<FieldSimilarityConfigVO> rules = configDTO.getFieldSimilarityls();
        if (CollectionUtils.isEmpty(rules)) {
            configDTO.setPlagiarismRules("");
        } else {
            configDTO.setPlagiarismRules(JSON.toJSONString(rules));
        }

        List<CustomerTypeRulesVO> customerTypeRules = new ArrayList<>();
        customerTypeRules = configDTO.getCustomerTypeRules();
        configDTO.setCustomerTypeRules(null);
        SystemRuleModuleConfigEntity configEntity = new SystemRuleModuleConfigEntity();
        BeanUtils.copyProperties(configDTO, configEntity);
        configEntity.setCustomerTypeRules(JSON.toJSONString(customerTypeRules));
        systemRuleModuleConfigService.saveOrUpdate(configEntity);
        return Result.result("修改成功");
    }

    @PostMapping(value = "/edit")
    @ApiOperation(value = "规则配置相关接口-编辑", notes = "规则配置相关接口-编辑")
    @Transactional(rollbackFor = Throwable.class)
    public Result<?> edit(@RequestBody SystemRuleModuleConfigDTO systemRuleModuleConfigDTO) {
        SystemRuleModuleConfigEntity sysRuleModuleConfigEntity = new SystemRuleModuleConfigEntity();
        BeanUtils.copyProperties(systemRuleModuleConfigDTO, sysRuleModuleConfigEntity);
        systemRuleModuleConfigService.saveOrUpdate(sysRuleModuleConfigEntity);
        return Result.result("修改成功");
    }

    @ApiOperation(value = "规则配置相关接口-删除", notes = "规则配置相关接口-删除")
    @PostMapping("delete")
    public Result delete(Long id) {
        if(Objects.isNull(id)) {
            return Result.error("参数错误");
        }
        systemRuleModuleConfigService.lgDelete(id);
        return Result.ok();
    }


    @PostMapping(value = "/getRuleField")
    @ApiOperation(value = "规则配置相关接口-字段配置：客户 10,线索 11 ,联系人 20", notes = "规则配置相关接口-字段配置")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SystemRuleModuleConfigDTO.class)
    })
    public Result<?> getRuleField(Integer resourceType) {
        LambdaQueryWrapper<SystemRuleFieldConfigEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemRuleFieldConfigEntity::getResourceType,resourceType);
        wrapper.eq(SystemRuleFieldConfigEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        List<SystemRuleFieldConfigEntity> ls = fieldConfigService.list(wrapper);
        return Result.result(ls);
    }
}
