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.CentralizedRequestDTO;
import digital.laboratory.platform.reagent.entity.CentralizedRequest;
import digital.laboratory.platform.reagent.entity.DetailsOfCentralized;
import digital.laboratory.platform.reagent.mapper.CentralizedRequestMapper;
import digital.laboratory.platform.reagent.service.CentralizedRequestService;
import digital.laboratory.platform.reagent.service.DetailsOfCentralizedService;
import digital.laboratory.platform.reagent.vo.CentralizedRequestVO;
import digital.laboratory.platform.reagent.vo.DetailsOfCentralizedVO;
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 (集中采购申请) 服务实现类
 */
@Service
@SuppressWarnings("all")
public class CentralizedRequestServiceImpl extends ServiceImpl<CentralizedRequestMapper, CentralizedRequest> implements CentralizedRequestService {

    @Autowired
    private CentralizedRequestService centralizedRequestService;

    @Autowired
    private DetailsOfCentralizedService detailsOfCentralizedService;

    @Transactional
    @Override//增加集中采购申请
    public CentralizedRequest addRequest( List<CentralizedRequestDTO> centralizedRequestDTOList, DLPUser dlpUser) {

        CentralizedRequest centralizedRequest = new CentralizedRequest();

        centralizedRequest.setApplicantId(dlpUser.getId());

        centralizedRequest.setCentralizedRequestId(IdWorker.get32UUID().toUpperCase());
        //获取申请时间
        centralizedRequest.setDateOfApplication(LocalDateTime.now());
        //未提交
        centralizedRequest.setStatus(0);

        centralizedRequest.setOrgName(dlpUser.getOrgName());

        centralizedRequest.setApplicantName(dlpUser.getName());

        List<DetailsOfCentralized> detailsOfCentralizedList = new ArrayList<>();
        //将DTO赋值给明细表
        for (CentralizedRequestDTO centralizedRequestDto : centralizedRequestDTOList) {

            DetailsOfCentralized detailsOfCentralized = new DetailsOfCentralized();

            BeanUtils.copyProperties(centralizedRequestDto, detailsOfCentralized);

            detailsOfCentralized.setDetailsOfCentralizedId(IdWorker.get32UUID().toUpperCase());

            detailsOfCentralized.setCentralizedRequestId(centralizedRequest.getCentralizedRequestId());

            detailsOfCentralizedList.add(detailsOfCentralized);
        }
        if (centralizedRequestService.save(centralizedRequest)&&detailsOfCentralizedService.saveBatch(detailsOfCentralizedList)){
            return centralizedRequest;
        }else return null;
    }


    @Override//提交申请
    public CentralizedRequest commitRequest(List<CentralizedRequestDTO> centralizedRequestDTOList,DLPUser dlpUser) {

        if (centralizedRequestDTOList.get(0).getCentralizedRequestId()==null){

            CentralizedRequest centralizedRequest = centralizedRequestService.addRequest(centralizedRequestDTOList, dlpUser);

            centralizedRequest.setStatus(1);

            if (centralizedRequestService.updateById(centralizedRequest)){
                return centralizedRequest;
            }else return null;
        }else {

            CentralizedRequest byId = centralizedRequestService.getById(centralizedRequestDTOList.get(0).getCentralizedRequestId());

            byId.setStatus(1);

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



    @Override//修改申请
    public DetailsOfCentralized editDetailsById(CentralizedRequestDTO centralizedRequestDto) {

        DetailsOfCentralized detailsOfCentralized = detailsOfCentralizedService.getById(centralizedRequestDto.getDetailsOfCentralizedId());

        BeanUtils.copyProperties(centralizedRequestDto, detailsOfCentralized);

        if (detailsOfCentralizedService.updateById(detailsOfCentralized)){
            return detailsOfCentralized;
        }else return null;

    }

    @Override//增加明细
    public DetailsOfCentralized addDetailsById(CentralizedRequestDTO centralizedRequestDto) {

        DetailsOfCentralized detailsOfCentralized = new DetailsOfCentralized();

        detailsOfCentralized.setDetailsOfCentralizedId(IdWorker.get32UUID().toUpperCase());

        BeanUtils.copyProperties(centralizedRequestDto, detailsOfCentralized);

        if (detailsOfCentralizedService.save(detailsOfCentralized)){
            return detailsOfCentralized;
        }else return null;
    }

    @Override//删除申请
    @Transactional
    public Boolean delRequestById(String centralizedRequestId) {

        LambdaQueryWrapper<DetailsOfCentralized> detailsOfCentralizedLambdaQueryWrapper = new LambdaQueryWrapper<>();

        detailsOfCentralizedLambdaQueryWrapper.eq(DetailsOfCentralized::getCentralizedRequestId, centralizedRequestId);

        List<DetailsOfCentralized> list = detailsOfCentralizedService.list(detailsOfCentralizedLambdaQueryWrapper);

        return detailsOfCentralizedService.removeBatchByIds(list)&&centralizedRequestService.removeById(centralizedRequestId);

    }


    @Override
    public IPage<CentralizedRequestVO> getCentralizedRequestVOPage(IPage<CentralizedRequest> page, QueryWrapper<CentralizedRequest> qw) {
        IPage<CentralizedRequestVO> r = baseMapper.getCentralizedRequestVOPage(page, qw);
        return r;
    }

    @Override
    public List<CentralizedRequestVO> getCentralizedRequestVOList(QueryWrapper<CentralizedRequest> qw) {

        LambdaQueryWrapper<CentralizedRequest> centralizedRequestLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //查询状态为1的采购申请：已提交
        centralizedRequestLambdaQueryWrapper.eq(CentralizedRequest::getStatus,1);

        List<CentralizedRequest> list = centralizedRequestService.list(centralizedRequestLambdaQueryWrapper);

        ArrayList<CentralizedRequestVO> centralizedRequestVOArrayList = new ArrayList<>();

        for (CentralizedRequest centralizedRequest : list) {
            CentralizedRequestVO centralizedRequestVO = new CentralizedRequestVO();
            BeanUtils.copyProperties(centralizedRequest,centralizedRequestVO);
            centralizedRequestVO.setDetailsOfCentralizedVOList(detailsOfCentralizedService.getDetailsOfCentralizedVOList(centralizedRequestVO.getCentralizedRequestId()));
            centralizedRequestVOArrayList.add(centralizedRequestVO);
        }

        return centralizedRequestVOArrayList;
    }

    @Override
    public CentralizedRequestVO getCentralizedRequestVO(String centralizedRequestId) {

        CentralizedRequestVO centralizedRequestVO = baseMapper.getCentralizedRequestVO(centralizedRequestId);

        List<DetailsOfCentralizedVO> detailsOfCentralizedVOList = detailsOfCentralizedService.getDetailsOfCentralizedVOList(centralizedRequestId);

        centralizedRequestVO.setDetailsOfCentralizedVOList(detailsOfCentralizedVOList);

        return centralizedRequestVO;
    }

    @Override
    public List<CentralizedRequestVO> getOrgVOList(QueryWrapper<CentralizedRequest> qw,String orgName) {

        LambdaQueryWrapper<CentralizedRequest> centralizedRequestLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //查询状态为1的采购申请：已提交
        centralizedRequestLambdaQueryWrapper.eq(CentralizedRequest::getStatus,1);

        centralizedRequestLambdaQueryWrapper.eq(CentralizedRequest::getOrgName,orgName);

        List<CentralizedRequest> list = centralizedRequestService.list(centralizedRequestLambdaQueryWrapper);

        ArrayList<CentralizedRequestVO> centralizedRequestVOArrayList = new ArrayList<>();

        for (CentralizedRequest centralizedRequest : list) {
            CentralizedRequestVO centralizedRequestVO = new CentralizedRequestVO();
            BeanUtils.copyProperties(centralizedRequest,centralizedRequestVO);
            centralizedRequestVO.setDetailsOfCentralizedVOList(detailsOfCentralizedService.getDetailsOfCentralizedVOList(centralizedRequestVO.getCentralizedRequestId()));
            centralizedRequestVOArrayList.add(centralizedRequestVO);
        }

        return centralizedRequestVOArrayList;
    }
}



