package org.jeecg.modules.sampleAndConsumableManagement.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.extension.plugins.pagination.Page;
import org.jeecg.modules.basedata.entity.FdPersonnel;
import org.jeecg.modules.projectManagement.entity.FdContract;
import org.jeecg.modules.sampleAndConsumableManagement.entity.FdColumnManagement;
import org.jeecg.modules.sampleAndConsumableManagement.entity.FdColumnManagementRecipientRecord;
import org.jeecg.modules.sampleAndConsumableManagement.mapper.FdColumnManagementMapper;
import org.jeecg.modules.basedata.service.*;
import org.jeecg.modules.projectManagement.service.IFdProjectFileService;
import org.jeecg.modules.sampleAndConsumableManagement.service.IFdColumnManagementRecipientRecordService;
import org.jeecg.modules.sampleAndConsumableManagement.service.IFdColumnManagementService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Description: 色谱柱管理
 * @author： jeecg-boot
 * @date： 2019-06-11
 * @version： V1.0
 */
@Service
@Transactional
public class FdColumnManagementServiceImpl extends ServiceImpl<FdColumnManagementMapper, FdColumnManagement> implements IFdColumnManagementService {

    @Resource
    IFdColumnManagementService fdColumnManagementService;

    @Resource
    IFdProjectFileService fdProjectFileService;

    @Resource
    IFdColumnManagementRecipientRecordService fdColumnManagementRecipientRecordService;

    @Resource
    IFdPersonnelService fdPersonnelService;

    @Resource
    FdColumnManagementMapper fdColumnManagementMapper;

    /**
     * @return void
     * @Author gurui
     * @Description //TODO 添加色谱柱管理 同时添加 色谱柱管理领用记录
     * @Date 9:14 2019/6/12
     * @Param [fdColumnManagement]
     **/
    @Override
    public void addAll(FdColumnManagement fdColumnManagement) {
        if (fdColumnManagement.getId() == null || fdColumnManagement.getId() == "") {
            fdColumnManagement.setDelFlag("0");
            fdColumnManagementService.save(fdColumnManagement);
        } else {
            fdColumnManagementService.updateById(fdColumnManagement);
            //删除色谱柱管理领用记录
            LambdaQueryWrapper<FdColumnManagementRecipientRecord> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(FdColumnManagementRecipientRecord::getPkColumnManagement, fdColumnManagement.getId());
            fdColumnManagementRecipientRecordService.remove(queryWrapper2);
        }
        // 保存管理负责人和项目负责人 名称
        LambdaQueryWrapper<FdPersonnel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FdPersonnel::getId, fdColumnManagement.getPkPersonInCharge());
        List<FdPersonnel> personnelList = fdPersonnelService.list(queryWrapper);
        for (FdPersonnel fdPersonnel : personnelList) {
            fdColumnManagement.setPersonInCharge(fdPersonnel.getName());
        }
        LambdaQueryWrapper<FdPersonnel> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(FdPersonnel::getId, fdColumnManagement.getPkProjectLeader());
        List<FdPersonnel> personnelList1 = fdPersonnelService.list(queryWrapper1);
        for (FdPersonnel fdPersonnel1 : personnelList1) {
            fdColumnManagement.setProjectLeader(fdPersonnel1.getName());
        }

        for (FdColumnManagementRecipientRecord fdColumnManagementRecipientRecord : fdColumnManagement.getFdColumnManagementRecipientRecordList()) {
            fdColumnManagementRecipientRecord.setId("");
            fdColumnManagementRecipientRecord.setPkColumnManagement(fdColumnManagement.getId());
        }
        //批量保存
        fdColumnManagementRecipientRecordService.saveBatch(fdColumnManagement.getFdColumnManagementRecipientRecordList());
    }

    @Override
    public FdColumnManagement queryColumnByConId(String pkProjectFile) {
        return fdColumnManagementMapper.queryColumnByConId(pkProjectFile);
    }

    /**
     * @return boolean
     * @Author gurui
     * @Description //TODO 删除色谱柱管理 同时删除色谱柱管理领用记录
     * @Date 13:25 2019/6/27
     * @Param [id]
     **/
    @Override
    public boolean deleteAll(String id) {
        boolean result = true;
        try {
           fdColumnManagementService.removeById(id);
           LambdaQueryWrapper<FdColumnManagementRecipientRecord> queryWrapper = new LambdaQueryWrapper<>();
           queryWrapper.eq(FdColumnManagementRecipientRecord::getPkColumnManagement,id);
           fdColumnManagementRecipientRecordService.remove(queryWrapper);
        } catch (Exception e) {
            result = false;
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public IPage<FdColumnManagement> findByPage(Page<FdColumnManagement> page, QueryWrapper<FdColumnManagement> queryWrapper) {
        return fdColumnManagementMapper.findByPage(page,queryWrapper);
    }

    @Override
    public List<FdContract> getFileContractList() {
        return fdColumnManagementMapper.getFileContractList();
    }

    @Override
    public String getContractPartyName(String id) {
        return fdColumnManagementMapper.getContractPartyName(id);
    }

    @Override
    public String getColumnTypeName(String id) {
        return fdColumnManagementMapper.getColumnTypeName(id);
    }

    @Override
    public String getParticleSizeName(String id) {
        return fdColumnManagementMapper.getParticleSizeName(id);
    }

    @Override
    public String getColumnLengthName(String id) {
        return fdColumnManagementMapper.getColumnLengthName(id);
    }

    @Override
    public String getColumnDiameter(String id) {
        return fdColumnManagementMapper.getColumnDiameter(id);
    }


}
