package cn.cslg.pas.service.impl;

import cn.cslg.pas.common.model.dto.AssoStructurePatentAddNewBatchDTO;
import cn.cslg.pas.common.model.dto.AssoStructurePatentQueryDTO;
import cn.cslg.pas.common.model.dto.AssoStructurePatentUpdateDTO;
import cn.cslg.pas.common.model.dto.StructureAndProductIds;
import cn.cslg.pas.common.model.vo.AssoStructurePatentVO;
import cn.cslg.pas.domain.Structure;
import cn.cslg.pas.domain.asso.AssoStructurePatent;
import cn.cslg.pas.exception.XiaoShiException;
import cn.cslg.pas.mapper.StructureMapper;
import cn.cslg.pas.mapper.asso.AssoStructurePatentMapper;
import cn.cslg.pas.service.IStructurePatentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

/**
 * 专题库产品架构专利号标引的Service层接口实现类
 *
 * @Author chenyu
 * @Date 2023/3/15
 */
@RequiredArgsConstructor
@Slf4j
@Service
public class StructurePatentServiceImpl extends ServiceImpl<AssoStructurePatentMapper, AssoStructurePatent> implements IStructurePatentService {
    private final AssoStructurePatentMapper assoStructurePatentMapper;
    private final StructureMapper structureMapper;

    /**
     * 更新标引(新增或删除)
     *
     * @param assoStructurePatentUpdateDTO 专题库产品架构专利号标引的修改DTO类对象
     */
    @Override
    public void update(AssoStructurePatentUpdateDTO assoStructurePatentUpdateDTO) {
        log.info("开始处理【更新标引】的业务，参数为：{}", assoStructurePatentUpdateDTO);

        //获取DTO中的updateType类型，true则为新增标引，false则为删除标引
        Integer structureId = assoStructurePatentUpdateDTO.getStructureId();
        Boolean updateType = assoStructurePatentUpdateDTO.getUpdateType();
        //DTO对象赋值给实体类
        AssoStructurePatent assoStructurePatent = new AssoStructurePatent();
        BeanUtils.copyProperties(assoStructurePatentUpdateDTO, assoStructurePatent);
        //获取架构路径path，赋值给实体类
        Structure structure = structureMapper.getStandardById(structureId);
        assoStructurePatent.setPath(structure.getPath());

        //true则新增标引，false则删除标引
        if (updateType) {
            log.info("架构专利关联表新增数据");
            int rows = assoStructurePatentMapper.insert(assoStructurePatent);
            if (rows != 1) {
                String message = "新增标引失败，服务器忙请稍后再次尝试";
                log.info("{}", message);
                throw new XiaoShiException(message);
            }
        } else {
            log.info("架构专利关联表删除数据");
            int rows = assoStructurePatentMapper.delete(assoStructurePatent);
            if (rows != 1) {
                String message = "删除标引失败，服务器忙请稍后再次尝试";
                log.info("{}", message);
                throw new XiaoShiException(message);
            }
        }

        log.info("更新标引完成");

    }

    /**
     * 批量新增标引
     *
     * @param assoStructurePatentAddNewBatchDTO 专题库批量新增标引(一个专题库给多个专利选择同样的多个产品架构)的DTO类
     */
    @Override
    public void addNewBatch(AssoStructurePatentAddNewBatchDTO assoStructurePatentAddNewBatchDTO) {
        log.info("开始处理【批量新增标引】的业务，参数为：{}", assoStructurePatentAddNewBatchDTO);

        //专题库id
        Integer projectId = assoStructurePatentAddNewBatchDTO.getProjectId();
        //多件专利
        List<String> patentNos = assoStructurePatentAddNewBatchDTO.getPatentNos();
        //多件专利共用的架构id和架构所属产品id
        List<StructureAndProductIds> structureAndProductIds = assoStructurePatentAddNewBatchDTO.getStructureAndProductIds();

        //DTO赋值给实体类
        ArrayList<AssoStructurePatent> newAssoStructurePatents = new ArrayList<>();
        for (String patentNo : patentNos) {
            for (StructureAndProductIds structureAndProductId : structureAndProductIds) {
                AssoStructurePatent assoStructurePatent = new AssoStructurePatent()
                        .setProjectId(projectId)
                        .setPatentNo(patentNo)
                        .setStructureId(structureAndProductId.getStructureId())
                        .setProductId(structureAndProductId.getProductId())
                        .setPath(structureMapper.getStandardById(structureAndProductId.getStructureId()).getPath());
                newAssoStructurePatents.add(assoStructurePatent);
            }
        }

        //根据专题库id从库中查询数据
        LambdaQueryWrapper<AssoStructurePatent> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AssoStructurePatent::getProjectId, projectId);
        List<AssoStructurePatent> oldAssoStructurePatents = this.list(wrapper);
        if (oldAssoStructurePatents != null && oldAssoStructurePatents.size() > 0) {
            //遍历查询出来的所有数据，将id改为Null
            for (AssoStructurePatent assoStructurePatent2 : oldAssoStructurePatents) {
                assoStructurePatent2.setId(null);
            }
        }

        //集合去重，去重后留下来的数据即为新增的标引就可以直接入库了
        boolean flag = newAssoStructurePatents.removeAll(oldAssoStructurePatents);
        //若有新增的标引则入库
        if (flag) {
            //数据批量插入架构和专利关联表
            log.info("数据批量插入架构和专利关联表");
            this.saveBatch(newAssoStructurePatents);
        }

    }

    /**
     * 查询标引
     *
     * @param assoStructurePatentQueryDTO 专题库产品架构专利号标引的查询DTO类
     * @return 返回查询到的数据
     */
    @Override
    public List<AssoStructurePatentVO> query(AssoStructurePatentQueryDTO assoStructurePatentQueryDTO) {
        log.info("开始处理【查询标引】的业务，参数为：{}", assoStructurePatentQueryDTO);
        List<AssoStructurePatentVO> queryResult = assoStructurePatentMapper.selectByProjectIdPatentNo(assoStructurePatentQueryDTO);
        return queryResult;
    }


}
