package pwc.taxtech.atms.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pwc.taxtech.atms.constant.enums.TbImportType;
import pwc.taxtech.atms.dao.PeriodInfoMapper;
import pwc.taxtech.atms.dto.OperationResultDto;
import pwc.taxtech.atms.entity.PeriodInfo;
import pwc.taxtech.atms.entity.PeriodInfoExample;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import static pwc.taxtech.atms.constant.Constant.FIRST_OR_DEFAULT;

@Service
public class ProjectInfoServiceImpl {
    private static final Logger LOGGER = LoggerFactory.getLogger(ProjectServiceImpl.class);
    @Autowired
    private PeriodInfoMapper periodInfoMapper;

    public OperationResultDto<Boolean> isProjectImportedData(String projectId, Integer serviceType, Integer importTypeId) {
        try {
            PeriodInfoExample example = new PeriodInfoExample();
            PeriodInfoExample.Criteria criteria = example.createCriteria().andProjectIdEqualTo(projectId).andImportTypeEqualTo(importTypeId);
            if (serviceType != null) criteria.andServiceTypeEqualTo(serviceType);

            List<PeriodInfo> piList = periodInfoMapper.selectByExample(example);

            OperationResultDto<Boolean> ord = new OperationResultDto<>();
            ord.setResult(piList != null & !piList.isEmpty());
            ord.setData(ord.getResult());
            return ord;
        } catch (Exception e) {
            LOGGER.warn("IsProjectImportedData method error", e);
            OperationResultDto<Boolean> ord = new OperationResultDto<>();
            ord.setResult(false);
            ord.setData(false);
            return ord;
        }
    }

    public OperationResultDto<Boolean> isProjectImportedData(String projectId, Integer importTypeId) {
        return isProjectImportedData(projectId, null, importTypeId);
    }

    public OperationResultDto<List<PeriodInfo>> getProjectImportType(String projectId, List<Integer> periods, Integer serviceTypeId, String id) {
        PeriodInfoExample example = new PeriodInfoExample();
        if (!periods.isEmpty()) example.createCriteria().andProjectIdEqualTo(projectId).andPeriodIn(periods);
        else example.createCriteria().andProjectIdEqualTo(projectId);
        List<PeriodInfo> existed = periodInfoMapper.selectByExample(example);

        periods.forEach(m -> {
            if (!existed.stream().anyMatch(n -> {
                return n.getPeriod().intValue() == m.intValue();
            })) {
                PeriodInfo info = new PeriodInfo();
                info.setId(UUID.randomUUID().toString());
                info.setProjectId(projectId);
                info.setPeriod(m);
                info.setStatus(0);
                info.setCreatorId(id);
                info.setImportType(TbImportType.UnImported.getCode());
                info.setServiceType(serviceTypeId);
                info.setCreateTime(new Date());
                info.setUpdateTime(new Date());
                periodInfoMapper.insert(info);
            }
        });
        example.createCriteria().andProjectIdEqualTo(projectId).andServiceTypeEqualTo(serviceTypeId);
        OperationResultDto<List<PeriodInfo>> resultDto = new OperationResultDto();
        resultDto.setResult(true);
        resultDto.setResultMsg("");
        resultDto.setData(periodInfoMapper.selectByExample(example));
        return resultDto;
    }

    public Integer getImportType(String projectId, Integer periodId, String userId, Integer serviceType) {
        PeriodInfoExample example = new PeriodInfoExample();
        example.createCriteria().andProjectIdEqualTo(projectId).andProjectIdEqualTo(projectId).andServiceTypeEqualTo(serviceType);

        List<PeriodInfo> periodInfoList = periodInfoMapper.selectByExample(example);
        if (periodInfoList == null || periodInfoList.size() == 0) {
            PeriodInfo periodInfo = new PeriodInfo();
            periodInfo.setId(UUID.randomUUID().toString());
            periodInfo.setProjectId(projectId);
            periodInfo.setPeriod(periodId);
            periodInfo.setStatus(0);
            periodInfo.setCreatorId(userId);
            periodInfo.setImportType(TbImportType.UnImported.getCode());
            periodInfo.setServiceType(serviceType);
            periodInfo.setCreateTime(new Date());
            periodInfo.setUpdateTime(new Date());

            periodInfoMapper.insert(periodInfo);
            return TbImportType.UnImported.getCode();
        } else {
            return periodInfoList.get(FIRST_OR_DEFAULT).getImportType();
        }

    }
}