package pwc.taxtech.atms.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pwc.taxtech.atms.dao.ProjectStatusManageMapper;
import pwc.taxtech.atms.dto.FieldsMapper;
import pwc.taxtech.atms.dto.OperationResultDto;
import pwc.taxtech.atms.dto.taxadmin.ProjectImportSubStatusDto;
import pwc.taxtech.atms.dto.taxadmin.ProjectStatusManageDto;
import pwc.taxtech.atms.entity.ProjectStatusManage;
import pwc.taxtech.atms.entity.ProjectStatusManageExample;

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

@Service
public class ProjectStatusManageServiceImpl {
    private static final int FIRST_OR_DFAULT = 0;

    private static Logger LOGGER = LoggerFactory.getLogger(ProjectStatusManageServiceImpl.class);

    @Autowired
    ProjectStatusManageMapper projectStatusManageMapper;

    public OperationResultDto<ProjectStatusManageDto> setProjectStatus(String projectId, Integer periodId, Integer status, String creatorId) {
        try {
            ProjectStatusManageExample example = new ProjectStatusManageExample();
            example.createCriteria().andProjectIdEqualTo(projectId).andPeriodIdEqualTo(periodId);
            List<ProjectStatusManage> psmList = projectStatusManageMapper.selectByExample(example);
            ProjectStatusManage psm = null;

            if (psmList != null && !psmList.isEmpty()) {
                psm = psmList.get(FIRST_OR_DFAULT);
                psm.setStatus(status);
                psm.setUpdateTime(new Date());

                projectStatusManageMapper.updateByPrimaryKey(psm);
            } else {
                psm = new ProjectStatusManage();
                psm.setId(UUID.randomUUID().toString());
                psm.setProjectId(projectId);
                psm.setPeriodId(periodId);
                psm.setStatus(status);
                psm.setCreatorId(creatorId);
                psm.setCreateTime(new Date());
                psm.setUpdateTime(new Date());

                projectStatusManageMapper.insert(psm);
            }

            OperationResultDto<ProjectStatusManageDto> ord = new OperationResultDto<>();
            ord.setResult(true);
            ord.setResultMsg("");
            ProjectStatusManageDto dto = new ProjectStatusManageDto();

            try {
                FieldsMapper.map(psm, dto);
            } catch (Exception e) {
                e.printStackTrace();
                LOGGER.warn("cast filed some error", e);
            }
            ord.setData(dto);
            return ord;
        } catch (Exception e) {
            LOGGER.error("ProjectStatusManageService,SetProjectStatus(string dbName, int periodId)发生错误: {}", e.getMessage());
            OperationResultDto<ProjectStatusManageDto> ord = new OperationResultDto<>();
            ord.setResult(false);
            ord.setResultMsg(e.getMessage());
            ord.setData(null);

            return ord;
        }
    }

    public OperationResultDto<ProjectStatusManageDto> getProjectStatus(String projectId, Integer periodId) {
        try {

            ProjectStatusManageExample example = new ProjectStatusManageExample();
            example.createCriteria().andPeriodIdEqualTo(periodId);
            List<ProjectStatusManage> psmList = projectStatusManageMapper.selectByExample(example);

            if (psmList != null && !psmList.isEmpty()) {
                ProjectStatusManage first = psmList.get(FIRST_OR_DFAULT);
                ProjectStatusManageDto dto = new ProjectStatusManageDto();

                try {
                    FieldsMapper.map(first, dto);
                } catch (Exception e) {
                    e.printStackTrace();
                    LOGGER.warn("field map some error");
                }

                ProjectImportSubStatusDto ssd = new ProjectImportSubStatusDto();//TODO:should query status from db (neo)
                ssd.setAdjustImport(false);
                ssd.setCustomInvoiceImport(false);
                ssd.setEntryImport(false);
                ssd.setErpImport(false);
                ssd.setInputInvoiceImport(false);
                ssd.setOutputInvoiceImport(false);
                ssd.setTbImport(false);
                ssd.setVoucherMapImport(false);
                dto.setImportSubStatus(ssd);

                OperationResultDto resultDto = new OperationResultDto();
                resultDto.setResult(true);
                resultDto.setResultMsg("");
                resultDto.setData(dto);

                return resultDto;
            } else {
                throw new Exception("数据库" + projectId + "状态信息为NULL");
            }

        } catch (Exception e) {
            OperationResultDto resultDto = new OperationResultDto();
            resultDto.setResult(false);
            resultDto.setResultMsg(e.getMessage());
            resultDto.setData(null);

            return resultDto;
        }

    }

    public OperationResultDto<ProjectStatusManage> isProjectStatusExisted(String projectId, int periodId) {
        ProjectStatusManageExample example = new ProjectStatusManageExample();
        example.createCriteria().andProjectIdEqualTo(projectId).andPeriodIdEqualTo(periodId);
        List<ProjectStatusManage> manage = projectStatusManageMapper.selectByExample(example);
        OperationResultDto<ProjectStatusManage> dto = new OperationResultDto();
        dto.setResult(true);
        dto.setResultMsg(StringUtils.EMPTY);
        dto.setData(manage.isEmpty() ? null : manage.get(0));
        return dto;
    }
}