package digital.laboratory.platform.reagent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import digital.laboratory.platform.common.mybatis.security.service.DLPUser;
import digital.laboratory.platform.reagent.dto.AuditAndApproveDTO;
import digital.laboratory.platform.reagent.dto.PurchaseCatalogueDTO;
import digital.laboratory.platform.reagent.entity.CatalogueDetails;
import digital.laboratory.platform.reagent.entity.PurchaseCatalogue;
import digital.laboratory.platform.reagent.entity.ReagentConsumables;
import digital.laboratory.platform.reagent.mapper.PurchaseCatalogueMapper;
import digital.laboratory.platform.reagent.service.CatalogueDetailsService;
import digital.laboratory.platform.reagent.service.PurchaseCatalogueService;
import digital.laboratory.platform.reagent.service.ReagentConsumablesService;
import digital.laboratory.platform.reagent.vo.PurchaseCatalogueVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * (采购目录)服务实现类
 *
 * @author Zhang Xiaolong created at  2023-03-10
 * @describe (采购目录) 服务实现类
 */
@SuppressWarnings("all")
@Service


public class PurchaseCatalogueServiceImpl extends ServiceImpl<PurchaseCatalogueMapper, PurchaseCatalogue> implements PurchaseCatalogueService {
    @Autowired
    private PurchaseCatalogueService purchaseCatalogueService;
    @Autowired
    private CatalogueDetailsService catalogueDetailsService;

    @Autowired
    private ReagentConsumablesService reagentConsumablesService;
@Autowired
    private CategoryTableServiceImpl typeTableService;

    @Override//新增采购目录
    @Transactional
    public PurchaseCatalogue addCatalogue(DLPUser dlpUser, List<PurchaseCatalogueDTO> purchaseCatalogueDTOList) {

        PurchaseCatalogue purchaseCatalogue = new PurchaseCatalogue();

        purchaseCatalogue.setPurchaseCatalogueId(IdWorker.get32UUID().toUpperCase());

        purchaseCatalogue.setStatus(0);

        List<CatalogueDetails> catalogueDetailsList = new ArrayList<>();

        for (PurchaseCatalogueDTO purchaseCatalogueDto : purchaseCatalogueDTOList) {

            CatalogueDetails catalogueDetails = new CatalogueDetails();

            BeanUtils.copyProperties(purchaseCatalogueDto, catalogueDetails);

            catalogueDetails.setCatalogueDetailsId(IdWorker.get32UUID().toUpperCase());

            catalogueDetails.setPurchaseCatalogueId(purchaseCatalogue.getPurchaseCatalogueId());

            catalogueDetailsList.add(catalogueDetails);

            typeTableService.addSpecies(catalogueDetails.getCategory(),catalogueDetails.getSpecies());
        }

        if (purchaseCatalogueService.save(purchaseCatalogue) & catalogueDetailsService.saveBatch(catalogueDetailsList)) {
            return purchaseCatalogue;
        } else return null;
    }


    @Override//修改采购目录
    public CatalogueDetails editCatalogue(PurchaseCatalogueDTO purchaseCatalogueDTO) {

        CatalogueDetails byId = catalogueDetailsService.getById(purchaseCatalogueDTO.getCatalogueDetailsId());

        BeanUtils.copyProperties(purchaseCatalogueDTO,byId);

        if (catalogueDetailsService.updateById(byId)){
            return byId;
        }else return null;
    }

    @Override//删除采购目录
    @Transactional
    public Boolean delCatalogue(String purchaseCatalogueId) {
        LambdaQueryWrapper<CatalogueDetails> catalogueDetailsLambdaQueryWrapper = new LambdaQueryWrapper<>();

        catalogueDetailsLambdaQueryWrapper.eq(CatalogueDetails::getPurchaseCatalogueId,purchaseCatalogueId);

        return catalogueDetailsService.removeBatchByIds(catalogueDetailsService.list(catalogueDetailsLambdaQueryWrapper))&&
        purchaseCatalogueService.removeById(purchaseCatalogueId);

    }
    @Override
    public CatalogueDetails addDetails(PurchaseCatalogueDTO purchaseCatalogueDTO){

        CatalogueDetails catalogueDetails = new CatalogueDetails();

        BeanUtils.copyProperties(purchaseCatalogueDTO,catalogueDetails);

        if (catalogueDetailsService.save(catalogueDetails)){

            return catalogueDetails;
        }else return null;
    }

    @Override
    public IPage<PurchaseCatalogueVO> getPurchaseCatalogueVOPage(IPage<PurchaseCatalogue> page, QueryWrapper<PurchaseCatalogue> qw) {

        IPage<PurchaseCatalogueVO> purchaseCatalogueVOPage = baseMapper.getPurchaseCatalogueVOPage(page, qw);

        List<PurchaseCatalogueVO> records = purchaseCatalogueVOPage.getRecords();

        for (PurchaseCatalogueVO record : records) {

            PurchaseCatalogue byId = purchaseCatalogueService.getById(record.getPurchaseCatalogueId());

            LambdaQueryWrapper<CatalogueDetails> catalogueDetailsLambdaQueryWrapper = new LambdaQueryWrapper<>();

            catalogueDetailsLambdaQueryWrapper.eq(CatalogueDetails::getPurchaseCatalogueId,byId.getPurchaseCatalogueId());

            List<CatalogueDetails> list = catalogueDetailsService.list(catalogueDetailsLambdaQueryWrapper);

            record.setQuantity(list.size());
        }

        return purchaseCatalogueVOPage;
    }

    @Override
    public List<PurchaseCatalogueVO> getPurchaseCatalogueVOList(QueryWrapper<PurchaseCatalogue> qw) {

        List<PurchaseCatalogueVO> purchaseCatalogueVOList = baseMapper.getPurchaseCatalogueVOList(qw);

        return purchaseCatalogueVOList;
    }

    @Override//通过ID查询
    public PurchaseCatalogueVO getPurchaseCatalogueVO(String purchaseCatalogueId) {

        PurchaseCatalogueVO purchaseCatalogueVO = baseMapper.getPurchaseCatalogueVO(purchaseCatalogueId);

        List<CatalogueDetails> catalogueDetailsList = catalogueDetailsService.getCatalogueDetailsList(purchaseCatalogueId);

        purchaseCatalogueVO.setCatalogueDetailsListList(catalogueDetailsList);

        purchaseCatalogueVO.setQuantity(catalogueDetailsList.size());

        return purchaseCatalogueVO;
    }

    @Override//提交目录
    public PurchaseCatalogue commitById(List<PurchaseCatalogueDTO> purchaseCatalogueDTOList, DLPUser dlpUser) {

        if (purchaseCatalogueDTOList.get(0).getPurchaseCatalogueId().equals("")) {

            PurchaseCatalogue purchaseCatalogue = purchaseCatalogueService.addCatalogue(dlpUser, purchaseCatalogueDTOList);

            purchaseCatalogue.setStatus(1);

            if (purchaseCatalogueService.updateById(purchaseCatalogue)) {
                return purchaseCatalogue;
            } else return null;

        } else {
            PurchaseCatalogue purchaseCatalogue = purchaseCatalogueService.getById(purchaseCatalogueDTOList.get(0).getPurchaseCatalogueId()
            );

            PurchaseCatalogue purchaseCatalogue1 = purchaseCatalogueService.addCatalogue(dlpUser, purchaseCatalogueDTOList);

            purchaseCatalogue1.setPurchaseCatalogueId(purchaseCatalogue.getPurchaseCatalogueId());

            purchaseCatalogue1.setStatus(1);

            purchaseCatalogueService.delCatalogue(purchaseCatalogue.getPurchaseCatalogueId());

            if (purchaseCatalogueService.updateById(purchaseCatalogue1)) {
                return purchaseCatalogue;
            } else return null;
        }
    }

    @Override//一级审核
    public PurchaseCatalogue primaryAuditCatalogue(AuditAndApproveDTO auditAndApproveDto, DLPUser dlpUser) {

        PurchaseCatalogue purchaseCatalogue = purchaseCatalogueService.getById(auditAndApproveDto.getUuId());

        purchaseCatalogue.setAuditOpinionOfPrimary(auditAndApproveDto.getAuditOpinion());

        purchaseCatalogue.setAuditResultOfPrimary(auditAndApproveDto.getAuditResult());

        purchaseCatalogue.setPrimaryAuditorId(dlpUser.getId());

        purchaseCatalogue.setAuditTimeOfPrimary(LocalDateTime.now());

       if (purchaseCatalogue.getAuditResultOfPrimary()==true){
           purchaseCatalogue.setStatus(2);}
       else {
           purchaseCatalogue.setStatus(-1);
       }

        if (purchaseCatalogueService.updateById(purchaseCatalogue)) {
            return purchaseCatalogue;
        } else return null;
    }

    @Transactional
    @Override//二级审核
    public PurchaseCatalogue secondaryAuditCatalogue(AuditAndApproveDTO auditAndApproveDto, DLPUser dlpUser) {

        PurchaseCatalogue purchaseCatalogue = purchaseCatalogueService.getById(auditAndApproveDto.getUuId());

        purchaseCatalogue.setAuditOpinionOfSecondary(auditAndApproveDto.getAuditOpinion());

        purchaseCatalogue.setAuditResultOfSecondary(auditAndApproveDto.getAuditResult());

        purchaseCatalogue.setSecondaryAuditorId(dlpUser.getId());

        purchaseCatalogue.setAuditTimeOfSecondary(LocalDateTime.now());

        if (purchaseCatalogue.getAuditResultOfSecondary()==true){
            purchaseCatalogue.setStatus(3);}
        else {
            purchaseCatalogue.setStatus(-1);
        }

        if (purchaseCatalogue.getAuditResultOfSecondary() == true) {
            //审核通过后，将采购目录添加的试剂耗材，录入至试剂耗材类
            LambdaQueryWrapper<CatalogueDetails> catalogueDetailsLambdaQueryWrapper = new LambdaQueryWrapper<>();

            catalogueDetailsLambdaQueryWrapper.eq(CatalogueDetails::getPurchaseCatalogueId, purchaseCatalogue.getPurchaseCatalogueId());

            List<CatalogueDetails> list = catalogueDetailsService.list(catalogueDetailsLambdaQueryWrapper);

            //目录中的试剂耗材是否存在试剂耗材类中，有则不管，无则添加
            for (CatalogueDetails catalogueDetails : list) {

                if (catalogueDetails.getReagentConsumableId() == null) {
                    catalogueDetails.setReagentConsumableId(IdWorker.get32UUID().toUpperCase());
                    catalogueDetailsService.updateById(catalogueDetails);
                }
                ReagentConsumables byId = reagentConsumablesService.getById(catalogueDetails.getReagentConsumableId());

                if (byId == null) {
                    ReagentConsumables reagentConsumables = reagentConsumablesService.addReagentConsumables(catalogueDetails);
                }

            }
        }
        if (purchaseCatalogueService.updateById(purchaseCatalogue)) {
            return purchaseCatalogue;
        } else return null;
    }

    @Override
    public PurchaseCatalogue releaseCatalogue(String purchaseCatalogueId) {

        PurchaseCatalogue purchaseCatalogue = purchaseCatalogueService.getById(purchaseCatalogueId);

        purchaseCatalogue.setStatus(4);

        if (purchaseCatalogueService.updateById(purchaseCatalogue)) {
            return purchaseCatalogue;
        } else return null;
    }

    @Override
    public PurchaseCatalogueVO getVOpage(){

        QueryWrapper<PurchaseCatalogue> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("status",4);

        QueryWrapper<PurchaseCatalogue> queryWrapper1 = queryWrapper.orderByDesc("create_time");

        List<PurchaseCatalogue> list = purchaseCatalogueService.list(queryWrapper1);

        PurchaseCatalogue purchaseCatalogue = list.get(0);

        if (purchaseCatalogue==null){
            throw new RuntimeException(String.format("未存在已发布的采购目录"));
        }

        PurchaseCatalogueVO purchaseCatalogueVO = purchaseCatalogueService.getPurchaseCatalogueVO(purchaseCatalogue.getPurchaseCatalogueId());

        return purchaseCatalogueVO;
    }
}
