package com.engine.salary.report.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.engine.core.impl.Service;
import com.engine.salary.constant.SalaryDefaultTenantConstant;
import com.engine.salary.enums.sicategory.DeleteTypeEnum;
import com.engine.salary.mapper.report.SalaryStatisticsEChartsSettingMapper;
import com.engine.salary.report.entity.dto.SalaryStatisticsEchartsDTO;
import com.engine.salary.report.entity.po.SalaryStatisticsEChartsSettingPO;
import com.engine.salary.report.service.SalaryStatisticsEchartsService;
import com.engine.salary.util.SalaryAssert;
import com.engine.salary.util.SalaryI18nUtil;
import com.engine.salary.util.db.MapperProxyFactory;
import com.engine.salary.util.db.IdGenerator;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 薪酬统计图表
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class SalaryStatisticsEchartsServiceImpl extends Service implements SalaryStatisticsEchartsService {

    private SalaryStatisticsEChartsSettingMapper getSalaryStatisticsDimensionMapper() {
        return MapperProxyFactory.getProxy(SalaryStatisticsEChartsSettingMapper.class);
    }
    @Override
    public String saveRangeSetting(SalaryStatisticsEchartsDTO saveParam) {
        SalaryAssert.notNull(saveParam.getReportId(), SalaryI18nUtil.getI18nLabel( 152562, "报表id不能为空"));
        SalaryAssert.notNull(saveParam.getChartsType(), SalaryI18nUtil.getI18nLabel( 177912, "报表类型不能为空"));

        SalaryStatisticsEChartsSettingPO salaryStatisticsEChartsSettingQue = null;
        List<SalaryStatisticsEChartsSettingPO> pos = getSalaryStatisticsDimensionMapper().listSome(SalaryStatisticsEChartsSettingPO.builder().reportId(saveParam.getReportId()).chartsType(saveParam.getChartsType()).build());
        if(CollectionUtils.isNotEmpty(pos)){
            salaryStatisticsEChartsSettingQue = pos.get(0);
        }

        if (salaryStatisticsEChartsSettingQue != null) {
            getSalaryStatisticsDimensionMapper().delete(salaryStatisticsEChartsSettingQue);
        }
        Date now = new Date();
        SalaryStatisticsEChartsSettingPO salaryStatisticsEChartsSetting = SalaryStatisticsEChartsSettingPO.builder()
                .id(IdGenerator.generate())
                .reportId(saveParam.getReportId())
                .chartsType(saveParam.getChartsType())
                .itemValues(JSONArray.toJSONString(saveParam.getItemValues()))
                .itemColValue(saveParam.getItemColValue())
                .dimensionRange(Objects.isNull(saveParam.getDimensionRange()) ? 0 : saveParam.getDimensionRange())
                .itemSortValue(saveParam.getItemSortValue())
                .itemColSortValue(saveParam.getItemColSortValue())
                .sortType(saveParam.getSortType())
                .sortNum(saveParam.getSortNum())
                .createTime(now)
                .updateTime(now)
                .deleteType(DeleteTypeEnum.NOT_DELETED.getValue())
                .creator((long)user.getUID())
                .tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
                .build();
        getSalaryStatisticsDimensionMapper().insertIgnoreNull(salaryStatisticsEChartsSetting);
        return StringUtils.EMPTY;
    }

    @Override
    public String deleteRangeSetting(Long reportId) {

        getSalaryStatisticsDimensionMapper().deleteByReportId(reportId);

        return StringUtils.EMPTY;
    }

    @Override
    public SalaryStatisticsEchartsDTO queryRangeSetting(SalaryStatisticsEchartsDTO resp) {


        SalaryStatisticsEChartsSettingPO salaryStatisticsEChartsSetting = null;
        List<SalaryStatisticsEChartsSettingPO> pos = getSalaryStatisticsDimensionMapper().listSome(SalaryStatisticsEChartsSettingPO.builder().reportId(resp.getReportId()).chartsType(resp.getChartsType()).build());
        if(CollectionUtils.isNotEmpty(pos)){
            salaryStatisticsEChartsSetting = pos.get(0);
        }

        if (salaryStatisticsEChartsSetting != null) {
            resp.setId(salaryStatisticsEChartsSetting.getId());
            resp.setReportId(salaryStatisticsEChartsSetting.getReportId());
            resp.setChartsType(salaryStatisticsEChartsSetting.getChartsType());
            resp.setDimensionRange(salaryStatisticsEChartsSetting.getDimensionRange());
            resp.setItemValues(JSONArray.parseArray(salaryStatisticsEChartsSetting.getItemValues(), String.class));
            resp.setSortNum(salaryStatisticsEChartsSetting.getSortNum());
            resp.setSortType(salaryStatisticsEChartsSetting.getSortType());
            resp.setItemColValue(salaryStatisticsEChartsSetting.getItemColValue());
            resp.setItemSortValue(salaryStatisticsEChartsSetting.getItemSortValue());
            resp.setItemColSortValue(salaryStatisticsEChartsSetting.getItemColSortValue());
            return resp;
        }
        return resp;
    }
}
