package com.huatek.busi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.dto.LabReportBasisDataStatisticsDto;
import com.huatek.busi.dto.LabReportParamDataDto;
import com.huatek.busi.dto.LabReportParamDataDtos;
import com.huatek.busi.entity.LabReportParamData;
import com.huatek.busi.mapper.LabReportParamDataMapper;
import com.huatek.busi.service.ILabReportParamDataService;
import com.huatek.busi.vo.LabReportParamDataVo;
import com.huatek.busi.where.LabReportBasisDataStatisticsWhereVo;
import com.huatek.busi.where.LabReportParamDataSelectVo;
import com.huatek.frame.base.service.DictionnaireDetailService;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.frame.base.util.Constants;
import com.huatek.frame.base.vo.DictionnaireDetailVO;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import com.huatek.torch.sql.dao.impl.BaseDao;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

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

@Component
@Service(interfaceClass = ILabReportParamDataService.class)
@Transactional(rollbackFor = Exception.class)
public class LabReportParamDataServiceImpl extends BaseDao<LabReportParamData, String> implements ILabReportParamDataService {

    @Resource
    private LabReportParamDataMapper labReportParamDataMapper;

    @Reference(check = false)
    DictionnaireDetailService dictionnaireDetailService;

    @Override
    public List<LabReportParamDataDtos> findList(Map<String, Object> map) {
        return labReportParamDataMapper.findList(map);
    }

    @Override
    public int delete(String eqType, String refId){
        if(Objects.equals(eqType, SiteConstants.ExternalTypeEnum.GIS.getValue()) || Objects.equals(eqType, SiteConstants.ExternalTypeEnum.BYQ.getValue())) {
            return labReportParamDataMapper.delete(refId);
        } else if(Objects.equals(eqType, SiteConstants.ExternalTypeEnum.BLQ.getValue())){
            return labReportParamDataMapper.deleteBlq(refId);
        } else if(Objects.equals(eqType, SiteConstants.ExternalTypeEnum.DL.getValue())){
            return labReportParamDataMapper.deleteDl(refId);
        } else if(Objects.equals(eqType, SiteConstants.ExternalTypeEnum.CW.getValue())){
            return labReportParamDataMapper.deleteKgg(refId);
        } else if(Objects.equals(eqType, SiteConstants.ExternalTypeEnum.GEN.getValue())){
            return labReportParamDataMapper.deleteGen(refId);
        }
        return 0;
    }

    @Override
    public int insert(LabReportParamDataVo labReportParamDataVo){
        LabReportParamData labReportParamData = new LabReportParamData();
        BeanUtils.copyProperties(labReportParamDataVo, labReportParamData);
        return this.add(labReportParamData);
    }

    @Override
    public Map<String, Object> findAll(LabReportParamDataSelectVo labReportParamDataSelectVo){
        labReportParamDataSelectVo.setId(labReportParamDataSelectVo.getId());
        labReportParamDataSelectVo.setDeleted(Constants.DEFAULT_NO);
        labReportParamDataSelectVo.setIsMark(Constants.IS_MARK);
        PageHelper.startPage(labReportParamDataSelectVo.getPage(), labReportParamDataSelectVo.getLimit());
        Page<LabReportParamDataDto> page = null;
        if(Objects.equals(labReportParamDataSelectVo.getEquType(), SiteConstants.ExternalTypeEnum.GIS.getValue()) || Objects.equals(labReportParamDataSelectVo.getEquType(), SiteConstants.ExternalTypeEnum.BYQ.getValue())) {
            page = labReportParamDataMapper.findAll(labReportParamDataSelectVo);
        } else if(Objects.equals(labReportParamDataSelectVo.getEquType(), SiteConstants.ExternalTypeEnum.BLQ.getValue())){
            page = labReportParamDataMapper.findBlqAll(labReportParamDataSelectVo);
        } else if(Objects.equals(labReportParamDataSelectVo.getEquType(), SiteConstants.ExternalTypeEnum.DL.getValue())){
            page = labReportParamDataMapper.findDlAll(labReportParamDataSelectVo);
        } else if(Objects.equals(labReportParamDataSelectVo.getEquType(), SiteConstants.ExternalTypeEnum.CW.getValue())){
            page = labReportParamDataMapper.findKggAll(labReportParamDataSelectVo);
        } else if(Objects.equals(labReportParamDataSelectVo.getEquType(), SiteConstants.ExternalTypeEnum.GEN.getValue())){
            page = labReportParamDataMapper.findGenAll(labReportParamDataSelectVo);
        }
        List<LabReportParamDataDto> list = null;
        if(page != null) {
            list = page.getResult();
            if (CollectionUtil.isNotEmpty(list)) {
                for (LabReportParamDataDto labReportParamDataDto : list) {

                    // 试验类别
                    String testType = labReportParamDataDto.getTestType();
                    if (StrUtil.isNotEmpty(testType) && !Objects.equals(testType, Constants.NO_DATA)) {
//                    DictionnaireDetailVO detailVO = dictionnaireDetailService.findByDCode(testType, "TEST_TYPE");
                        labReportParamDataDto.setTestTypeName(testType);
                    } else {
                        labReportParamDataDto.setTestTypeName(Constants.NO_DATA);
                    }

                    // 单位
                    String unit = labReportParamDataDto.getUnit();
                    if (StrUtil.isNotEmpty(unit) && !Objects.equals(unit, Constants.NO_DATA)) {
                        DictionnaireDetailVO detailVO1 = dictionnaireDetailService.findByDCode(unit, "IEC_O_UNIT");
                        labReportParamDataDto.setDname(detailVO1.getDname());
                    } else {
                        labReportParamDataDto.setDname(Constants.NO_DATA);
                    }

                    // 试验结果
                    String testResult = labReportParamDataDto.getTestResult();
                    if (StrUtil.isNotEmpty(testResult) && !Objects.equals(testResult, Constants.NO_DATA)) {
                        DictionnaireDetailVO detailVO2 = dictionnaireDetailService.findByDCode(testResult, "TEST_RESULT");
                        labReportParamDataDto.setTestResultName(detailVO2.getDname());
                    } else {
                        labReportParamDataDto.setTestResultName(Constants.NO_DATA);
                    }

                    // 是否打标
                    String isMark = labReportParamDataDto.getIsMark();
                    if (StrUtil.isNotEmpty(isMark) && !Objects.equals(isMark, Constants.NO_DATA)) {
                        DictionnaireDetailVO detailVO3 = dictionnaireDetailService.findByDCode(isMark, "IS_MARK");
                        labReportParamDataDto.setMarkName(detailVO3.getDname());
                    } else {
                        labReportParamDataDto.setMarkName(Constants.NO_DATA);
                    }
                }
            }
        }
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, page.getTotal(), "BUSINESS.QUERY.SUCCESS", list);
    }

    @Override
    public List<LabReportBasisDataStatisticsDto> statistics(LabReportBasisDataStatisticsWhereVo labReportBasisDataStatisticsVo){
        labReportBasisDataStatisticsVo.setDeleted(Constants.DEFAULT_NO);
        return labReportParamDataMapper.statistics(labReportBasisDataStatisticsVo);
    }


    @Override
    public int updateByRefIds(List<String> refIds , String eqType){
        if(Objects.equals(eqType, SiteConstants.ExternalTypeEnum.GIS.getValue()) || Objects.equals(eqType, SiteConstants.ExternalTypeEnum.BYQ.getValue())) {
            return labReportParamDataMapper.updateByRefIds(refIds);
        } else if(Objects.equals(eqType, SiteConstants.ExternalTypeEnum.BLQ.getValue())){
            return labReportParamDataMapper.updateBlqByRefIds(refIds);
        } else if(Objects.equals(eqType, SiteConstants.ExternalTypeEnum.DL.getValue())){
            return labReportParamDataMapper.updateDlByRefIds(refIds);
        } else if(Objects.equals(eqType, SiteConstants.ExternalTypeEnum.CW.getValue())){
            return labReportParamDataMapper.updateKggByRefIds(refIds);
        } else if(Objects.equals(eqType, SiteConstants.ExternalTypeEnum.GEN.getValue())){
            return labReportParamDataMapper.updateGenByRefIds(refIds);
        }
        return 0;
    }
}
