package cn.cecep.talroad.service.impl;

import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.domain.PcLabel;
import cn.cecep.talroad.domain.PcLabelFactory;
import cn.cecep.talroad.domain.PcLabelValue;
import cn.cecep.talroad.enums.LabelFactoryEnum;
import cn.cecep.talroad.mapper.PcLabelFactoryMapper;
import cn.cecep.talroad.mapper.PcLabelMapper;
import cn.cecep.talroad.mapper.PcLabelValueMapper;
import cn.cecep.talroad.service.IPcLabelValueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.UUID;


/**
 * 特征标签-参数值Service业务层处理
 *
 * @author szhpt
 * @date 2023-02-02*/
@Service
public class PcLabelValueServiceImpl extends ServiceImpl<PcLabelValueMapper,PcLabelValue> implements IPcLabelValueService
{
    @Autowired
    private PcLabelValueMapper pcLabelValueMapper;
    @Autowired
    private PcLabelMapper pcLabelMapper;

    @Autowired
    private PcLabelFactoryMapper labelFactoryMapper;

    /**
     * 查询特征标签-参数值列表
     *
     * @param pcLabelValue 特征标签-参数值
     * @return 特征标签-参数值
     */
    @Override
    public List<PcLabelValue> selectPcLabelValueList(PcLabelValue pcLabelValue)
    {
        return pcLabelValueMapper.selectPcLabelValueList(pcLabelValue);
    }

    @Override
    public List<PcLabelValue> manualMonitoringIndustry(String name) {
            PcLabel pcLabel = pcLabelMapper.selectOne(new QueryWrapper<PcLabel>().eq("label_code", "BASE001"));
            QueryWrapper<PcLabelValue> pcLabelValueQueryWrapper = new QueryWrapper<>();
            pcLabelValueQueryWrapper.eq("label_id",pcLabel.getId());
            pcLabelValueQueryWrapper.like(StringUtils.isNotEmpty(name),"arguments_name",name);
            List<PcLabelValue> pcLabelValueList = pcLabelValueMapper.selectList(pcLabelValueQueryWrapper);
            return pcLabelValueList;
    }

    @Override
    public List<PcLabelValue> selectListByLabelId(String labelId){
        QueryWrapper<PcLabelValue> pcLabelValueQueryWrapper = new QueryWrapper<>();
        pcLabelValueQueryWrapper.eq("label_id",labelId);
        return pcLabelValueMapper.selectList(pcLabelValueQueryWrapper);
    }

    @Override
    public void add(PcLabelValue pcLabelValue) {
        if (StringUtils.isBlank(pcLabelValue.getArgumentsName())) {
            throw new RuntimeException("参数名称不可以为空");
        }
        if (StringUtils.isBlank(pcLabelValue.getArgumentsCode())) {
            throw new RuntimeException("参数编码不可以为空");
        }
        if (StringUtils.isBlank(pcLabelValue.getLabelId())) {
            throw new RuntimeException("labelId不可以为空");
        }

        PcLabel pcLabel = pcLabelMapper.selectById(pcLabelValue.getLabelId());

        if (StringUtils.isNotEmpty(pcLabel.getFactoryField()) || LabelFactoryEnum.getByCode(pcLabel.getLabelCode()) != null) {
            throw new RuntimeException("非手工标签，不支持新增标签值");
        }

        List<PcLabelValue> list = list(new QueryWrapper<PcLabelValue>()
                .eq("label_id", pcLabelValue.getLabelId())
                .eq("arguments_code", pcLabelValue.getArgumentsCode()));
        if (list.size() > 0) {
            throw new RuntimeException("参数编码重复");
        }

        List<PcLabelValue> list2 = list(new QueryWrapper<PcLabelValue>()
                .eq("label_id", pcLabelValue.getLabelId())
                .eq("arguments_name", pcLabelValue.getArgumentsName()));
        if (list2.size() > 0) {
            throw new RuntimeException("参数名称重复");
        }
        pcLabelValue.setId(UUID.randomUUID().toString());
        save(pcLabelValue);
    }

    @Override
    public void edit(PcLabelValue pcLabelValue) {
        PcLabelValue dbLableVale = getById(pcLabelValue.getId());
        if (dbLableVale == null) {
            return;
        }
        if (StringUtils.isBlank(pcLabelValue.getArgumentsName())) {
            throw new RuntimeException("参数名称不可以为空");
        }
        if (StringUtils.isBlank(pcLabelValue.getArgumentsCode())) {
            throw new RuntimeException("参数编码不可以为空");
        }

        PcLabel pcLabel = pcLabelMapper.selectById(dbLableVale.getLabelId());

        // 标签编码被修改
        if (!Objects.equals(dbLableVale.getArgumentsCode(), pcLabelValue.getArgumentsCode())) {
            if (StringUtils.isNotEmpty(pcLabel.getFactoryField()) || LabelFactoryEnum.getByCode(pcLabel.getLabelCode()) != null) {
                throw new RuntimeException("非手工标签，不支持修改标签参数编码");
            } else {
                Long count = labelFactoryMapper.selectCountByLableValue(dbLableVale);
                if (count > 0) {
                    throw new RuntimeException("标签值参数编码已被引用，不可以修改");
                }

                List<PcLabelValue> list = list(new QueryWrapper<PcLabelValue>()
                        .eq("label_id", pcLabelValue.getLabelId())
                        .eq("arguments_code", pcLabelValue.getArgumentsCode())
                        .ne("id", pcLabelValue.getId())
                );
                if (list.size() > 0) {
                    throw new RuntimeException("参数编码重复");
                }
            }
        }

        // 标签参数名称被修改
        if (!Objects.equals(dbLableVale.getArgumentsName(), pcLabelValue.getArgumentsName())) {
            if (StringUtils.isNotEmpty(pcLabel.getFactoryField()) || LabelFactoryEnum.getByCode(pcLabel.getLabelCode()) != null) {
                throw new RuntimeException("非手工标签，不支持修改标签参数名称");
            } else {
                List<PcLabelValue> list2 = list(new QueryWrapper<PcLabelValue>()
                        .eq("label_id", pcLabelValue.getLabelId())
                        .eq("arguments_name", pcLabelValue.getArgumentsName())
                        .ne("id", pcLabelValue.getId())
                );
                if (list2.size() > 0) {
                    throw new RuntimeException("参数名称重复");
                }
            }
        }

        // 忽略页面传入的labelId，该属性不可以更新
        pcLabelValue.setLabelId(null);

        updateById(pcLabelValue);
    }

    @Override
    public void delete(String id) {
        PcLabelValue labelValue = getById(id);
        if (labelValue == null) {
            return;
        }

        PcLabel pcLabel = pcLabelMapper.selectById(labelValue.getLabelId());
        if (StringUtils.isNotEmpty(pcLabel.getFactoryField()) || LabelFactoryEnum.getByCode(pcLabel.getLabelCode()) != null) {
            throw new RuntimeException("非手工标签，不支持删除标签值");
        }

        Long count = labelFactoryMapper.selectCountByLableValue(labelValue);
        if (count > 0) {
            throw new RuntimeException("标签值参数编码已被引用，不可以删除");
        }
        removeById(labelValue);
    }
}
