package com.xinelu.manage.service.labelfieldcontent.impl;

import com.xinelu.common.constant.Constants;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.common.utils.bean.BeanUtils;
import com.xinelu.common.utils.codes.GenerateSystemCodeUtil;
import com.xinelu.manage.domain.labelfieldcontent.LabelFieldContent;
import com.xinelu.manage.dto.labelfieldcontent.LabelFieldContentAddDTO;
import com.xinelu.manage.dto.labelfieldcontent.LabelFieldContentDTO;
import com.xinelu.manage.mapper.labelfieldcontent.LabelFieldContentMapper;
import com.xinelu.manage.service.labelfieldcontent.ILabelFieldContentService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 标签字段内容信息Service业务层处理
 *
 * @author xinelu
 * @date 2024-03-11
 */
@Service
public class LabelFieldContentServiceImpl implements ILabelFieldContentService {
    @Resource
    private LabelFieldContentMapper labelFieldContentMapper;

    @Resource
    private GenerateSystemCodeUtil systemCodeUtil;

    /**
     * 查询标签字段内容信息
     *
     * @param id 标签字段内容信息主键
     * @return 标签字段内容信息
     */
    @Override
    public LabelFieldContent selectLabelFieldContentById(Long id) {
        return labelFieldContentMapper.selectLabelFieldContentById(id);
    }

    /**
     * 查询标签字段内容信息列表
     *
     * @param labelFieldContent 标签字段内容信息
     * @return 标签字段内容信息
     */
    @Override
    public List<LabelFieldContent> selectLabelFieldContentList(LabelFieldContent labelFieldContent) {
        return labelFieldContentMapper.selectLabelFieldContentList(labelFieldContent);
    }

    /**
     * 新增标签字段内容信息
     *
     * @param labelFieldContent 标签字段内容信息
     * @return 结果
     */
    @Override
    public int insertLabelFieldContent(LabelFieldContent labelFieldContent) {
        labelFieldContent.setCreateTime(LocalDateTime.now());
        return labelFieldContentMapper.insertLabelFieldContent(labelFieldContent);
    }

    /**
     * 修改标签字段内容信息
     *
     * @param labelFieldContent 标签字段内容信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateLabelFieldContent(LabelFieldContent labelFieldContent) {
        if (ObjectUtils.isEmpty(labelFieldContentMapper.selectLabelFieldContentById(labelFieldContent.getId()))) {
            throw new ServiceException("当前字段内容不存在，无法修改，请联系管理员");
        }
        // 检查除去当前记录外有没有重复内容名称
        int existCount = labelFieldContentMapper.existCountByContentNameExcludingId(labelFieldContent.getId(), labelFieldContent.getFieldId(), labelFieldContent.getContentName());
        if (existCount > 0) {
            throw new ServiceException("当前标签字段类型下字段内容名称已存在");
        }
        labelFieldContent.setUpdateBy(SecurityUtils.getUsername());
        labelFieldContent.setUpdateTime(LocalDateTime.now());
        return labelFieldContentMapper.updateLabelFieldContent(labelFieldContent);
    }

    /**
     * 批量删除标签字段内容信息
     *
     * @param ids 需要删除的标签字段内容信息主键
     * @return 结果
     */
    @Override
    public int deleteLabelFieldContentByIds(Long[] ids) {
        return labelFieldContentMapper.deleteLabelFieldContentByIds(ids);
    }

    /**
     * 删除标签字段内容信息信息
     *
     * @param id 标签字段内容信息主键
     * @return 结果
     */
    @Override
    public int deleteLabelFieldContentById(Long id) {
        return labelFieldContentMapper.deleteLabelFieldContentById(id);
    }

    /**
     * 批量新增标签字段内容信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertLabelFieldContents(LabelFieldContentAddDTO labelFieldContentAddDTO) {
        // 获取到标签字段内容信息集合
        List<LabelFieldContentDTO> fieldContentList = labelFieldContentAddDTO.getFieldContentList();
        // 获取到标签字段信息Id
        Long fieldId = labelFieldContentAddDTO.getFieldId();
        // 检查数据库中同一个fieldId下内容名称的重复性
        for (LabelFieldContentDTO contentDTO : fieldContentList) {
            // 如果存在，报错-已存在字段内容名称
            if (labelFieldContentMapper.existCountByFieldIdAndContentName(fieldId, contentDTO.getContentName()) > 0) {
                throw new ServiceException("当前标签字段类型下字段内容名称已存在");
            }
            // 不存在，执行新增
            LabelFieldContent labelFieldContent = new LabelFieldContent();
            BeanUtils.copyProperties(contentDTO, labelFieldContent);
            labelFieldContent.setFieldId(labelFieldContentAddDTO.getFieldId());
            labelFieldContent.setFieldName(labelFieldContentAddDTO.getFieldName());
            labelFieldContent.setContentCode(Constants.CONTENT_ENCODING + systemCodeUtil.generateDepartCode(Constants.CONTENT_ENCODING));
            labelFieldContent.setCreateBy(SecurityUtils.getUsername());
            labelFieldContent.setCreateTime(LocalDateTime.now());
            if (labelFieldContentMapper.insertLabelFieldContent(labelFieldContent) <= 0) {
                throw new ServiceException("新增标签字段内容信息失败");
            }
        }
        return 1;
    }
}
