package com.microservices.pms.product.service.impl;

import com.microservices.common.core.exception.ServiceException;
import com.microservices.common.core.utils.DateUtils;
import com.microservices.common.core.utils.PageUtils;
import com.microservices.common.core.utils.StringUtils;
import com.microservices.common.core.web.page.GenericsTableDataInfo;
import com.microservices.common.security.utils.SecurityUtils;
import com.microservices.pms.common.service.IPmsCommonAsyncService;
import com.microservices.pms.enterprise.domain.PmsEnterprise;
import com.microservices.pms.enterprise.service.IPmsEnterpriseService;
import com.microservices.pms.product.domain.PmsProductRequirementTag;
import com.microservices.pms.product.mapper.PmsProductRequirementTagMapper;
import com.microservices.pms.product.service.IPmsProductRequirementService;
import com.microservices.pms.product.service.IPmsProductRequirementTagService;
import com.microservices.pms.utils.PmsConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

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

/**
 * 产品需求标记Service业务层处理
 * 
 * @author otto
 * @date 2023-10-10
 */
@Service
public class PmsProductRequirementTagServiceImpl implements IPmsProductRequirementTagService 
{
    @Autowired
    private PmsProductRequirementTagMapper pmsProductRequirementTagMapper;
    @Autowired
    private IPmsEnterpriseService pmsEnterpriseService;
    @Autowired
    @Lazy
    private IPmsProductRequirementService pmsProductRequirementService;
    @Autowired
    private IPmsCommonAsyncService pmsCommonAsyncServiceImpl;

    /**
     * 查询产品需求标记
     * 
     * @param id 产品需求标记主键
     * @return 产品需求标记
     */
    @Override
    public PmsProductRequirementTag selectPmsProductRequirementTagById(Long id)
    {
        PmsProductRequirementTag pmsProductRequirementTag=pmsProductRequirementTagMapper.selectPmsProductRequirementTagById(id);
        if(pmsProductRequirementTag==null){
            throw new ServiceException("该标记不存在(标记Id[%d])", id);
        }
        pmsEnterpriseService.selectPmsEnterpriseById(pmsProductRequirementTag.getPmsEnterpriseId());
        return pmsProductRequirementTag;
    }

    /**
     * 查询产品需求标记列表
     *
     * @param enterpriseIdentifier
     * @param pmsProductRequirementTag 产品需求标记
     * @param type
     * @return 产品需求标记
     */
    @Override
    public GenericsTableDataInfo<PmsProductRequirementTag> selectPmsProductRequirementTagList(String enterpriseIdentifier, PmsProductRequirementTag pmsProductRequirementTag, int type)
    {
        PmsEnterprise pmsEnterprise = pmsEnterpriseService.selectPmsEnterpriseByIdentifier(enterpriseIdentifier);
        pmsProductRequirementTag.setPmsEnterpriseId(pmsEnterprise.getId());
        pmsProductRequirementTag.setType(type);
        return PageUtils.toPage(pmsProductRequirementTagMapper.selectPmsProductRequirementTagList(pmsProductRequirementTag));
    }

    /**
     * 新增产品需求标记
     *
     * @param pmsProductRequirementTag 产品需求标记
     * @param type
     * @return 结果
     */
    @Override
    public int insertPmsProductRequirementTag(PmsProductRequirementTag pmsProductRequirementTag, int type)
    {
        // 校验企业是否存在同时校验数据权限
        pmsEnterpriseService.selectPmsEnterpriseById(pmsProductRequirementTag.getPmsEnterpriseId());
        pmsProductRequirementTag.setType(type);
        checkNameDuplicatesByEnterprise(pmsProductRequirementTag.getTagName(), pmsProductRequirementTag.getPmsEnterpriseId(), type);

        pmsProductRequirementTag.setCreateBy(SecurityUtils.getUsername());
        pmsProductRequirementTag.setCreateTime(DateUtils.getNowDate());
        return pmsProductRequirementTagMapper.insertPmsProductRequirementTag(pmsProductRequirementTag);
    }

    private void checkNameDuplicatesByEnterprise(String name, Long pmsEnterpriseId, int type) {
        PmsProductRequirementTag tag = pmsProductRequirementTagMapper.selectPmsProductRequirementTagByNameAndEnterpriseId(name, pmsEnterpriseId, type);
        if (tag != null) {
            throw new ServiceException("该标记名称在当前企业下已存在(标记名称[%s])", name);
        }
    }

    /**
     * 修改产品需求标记
     *
     * @param pmsProductRequirementTag 产品需求标记
     * @return 结果
     */
    @Override
    public int updatePmsProductRequirementTag(PmsProductRequirementTag pmsProductRequirementTag)
    {
        PmsProductRequirementTag oldPmsProductRequirementTag = selectPmsProductRequirementTagById(pmsProductRequirementTag.getId());
        if(StringUtils.isNotEmpty(pmsProductRequirementTag.getTagName())
                &&!pmsProductRequirementTag.getTagName().equals(oldPmsProductRequirementTag.getTagName())){
            checkNameDuplicatesByEnterprise(pmsProductRequirementTag.getTagName(), oldPmsProductRequirementTag.getPmsEnterpriseId(), oldPmsProductRequirementTag.getType());
        }
        pmsProductRequirementTag.setUpdateBy(SecurityUtils.getUsername());
        pmsProductRequirementTag.setUpdateTime(DateUtils.getNowDate());
        return pmsProductRequirementTagMapper.updatePmsProductRequirementTag(pmsProductRequirementTag);
    }

    /**
     * 批量删除产品需求标记
     * 
     * @param ids 需要删除的产品需求标记主键
     * @return 结果
     */
    @Override
    public int deletePmsProductRequirementTagByIds(Long[] ids)
    {
        int success = 0;
        for (Long id : ids) {
            success += deletePmsProductRequirementTagById(id);
        }
        return success;
    }

    /**
     * 删除产品需求标记信息
     * 
     * @param id 产品需求标记主键
     * @return 结果
     */
    @Override
    public int deletePmsProductRequirementTagById(Long id)
    {
        PmsProductRequirementTag pmsProductRequirementTag = selectPmsProductRequirementTagById(id);
        if (pmsProductRequirementTag.getType().equals(PmsConstants.PRODUCT_TAG_AND_MODULE_TYPE)) {
            // 在移除产品需求标签时，应同时移除所有与该标签关联的产品需求上的该标签。
            pmsCommonAsyncServiceImpl.asyncClearProductRequirementTag(pmsProductRequirementTag.getId());
        } else if (pmsProductRequirementTag.getType().equals(PmsConstants.TESTCASE_TAG_AND_MODULE_TYPE)) {
            pmsCommonAsyncServiceImpl.asyncClearTestCaseTag(pmsProductRequirementTag.getId());
        }
        return pmsProductRequirementTagMapper.deletePmsProductRequirementTagById(id);
    }

    @Override
    public void insertDefaultPmsProductRequirementTag(Long pmsEnterpriseId) {
        PmsProductRequirementTag pmsProductRequirementTag = new PmsProductRequirementTag();
        pmsProductRequirementTag.setType(PmsConstants.PRODUCT_TAG_AND_MODULE_TYPE);
        pmsProductRequirementTag.setCreateBy(SecurityUtils.getUsername());
        pmsProductRequirementTag.setCreateTime(DateUtils.getNowDate());
        pmsProductRequirementTag.setPmsEnterpriseId(pmsEnterpriseId);
        pmsProductRequirementTag.setColor("#d92d4c");
        pmsProductRequirementTag.setTagName("缺陷");
        pmsProductRequirementTagMapper.insertPmsProductRequirementTag(pmsProductRequirementTag);
        pmsProductRequirementTag.setColor("#ee955a");
        pmsProductRequirementTag.setTagName("功能");
        pmsProductRequirementTagMapper.insertPmsProductRequirementTag(pmsProductRequirementTag);
        pmsProductRequirementTag.setColor("#2d6ddc");
        pmsProductRequirementTag.setTagName("疑问");
        pmsProductRequirementTagMapper.insertPmsProductRequirementTag(pmsProductRequirementTag);
        pmsProductRequirementTag.setColor("#019549");
        pmsProductRequirementTag.setTagName("支持");
        pmsProductRequirementTagMapper.insertPmsProductRequirementTag(pmsProductRequirementTag);
        pmsProductRequirementTag.setColor("#c1a30d");
        pmsProductRequirementTag.setTagName("任务");
        pmsProductRequirementTagMapper.insertPmsProductRequirementTag(pmsProductRequirementTag);
        pmsProductRequirementTag.setColor("#2a0dc1");
        pmsProductRequirementTag.setTagName("协助");
        pmsProductRequirementTagMapper.insertPmsProductRequirementTag(pmsProductRequirementTag);
        pmsProductRequirementTag.setColor("#892794");
        pmsProductRequirementTag.setTagName("搁置");
        pmsProductRequirementTagMapper.insertPmsProductRequirementTag(pmsProductRequirementTag);
        pmsProductRequirementTag.setColor("#9ed600");
        pmsProductRequirementTag.setTagName("文档");
        pmsProductRequirementTagMapper.insertPmsProductRequirementTag(pmsProductRequirementTag);
        pmsProductRequirementTag.setColor("#2897b9");
        pmsProductRequirementTag.setTagName("测试");
        pmsProductRequirementTagMapper.insertPmsProductRequirementTag(pmsProductRequirementTag);
        pmsProductRequirementTag.setColor("#bb5332");
        pmsProductRequirementTag.setTagName("重复");
        pmsProductRequirementTagMapper.insertPmsProductRequirementTag(pmsProductRequirementTag);
    }


    @Override
    public void checkTags(String tags, Integer type, Long pmsEnterpriseId) {
        if (StringUtils.isBlank(tags)) {
            return;
        }
        String[] tagNameList = tags.split(",");
        for (String tagIdStr : tagNameList) {
            if (StringUtils.isNotBlank(tagIdStr)) {
                try {
                    Long tagId = Long.valueOf(tagIdStr);
                    PmsProductRequirementTag pmsProductRequirementTag = selectPmsProductRequirementTagById(tagId);
                    if (!Objects.equals(type, pmsProductRequirementTag.getType())) {
                        throw new ServiceException("标记类型错误(标记名称[%s])", pmsProductRequirementTag.getTagName());
                    }
                    if (!pmsEnterpriseId.equals(pmsProductRequirementTag.getPmsEnterpriseId())) {
                        throw new ServiceException("该标记不属于当前企业(标记名称[%s],企业id[%s])", pmsProductRequirementTag.getTagName(), pmsEnterpriseId);
                    }
                } catch (NumberFormatException e) {
                    throw new ServiceException("标记Id格式错误");
                }
            }
        }
    }

    @Override
    public List<PmsProductRequirementTag> selectTagListByIds(String tagIds) {
        if (StringUtils.isEmpty(tagIds)) {
            return new ArrayList<>();
        }
        String[] tagIdList = tagIds.split(",");
        return pmsProductRequirementTagMapper.selectPmsProductRequirementTagListByTagIds(tagIdList);
    }
}
