
package org.sky.scientific.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.sky.core.log.exception.ServiceException;
import org.sky.core.secure.utils.SecureUtil;
import org.sky.core.tool.utils.DateUtil;
import org.sky.core.tool.utils.Func;
import org.sky.scientific.mapper.SettingMapper;
import org.sky.scientific.pojo.entity.SettingEntity;
import org.sky.scientific.service.SettingService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  设置 服务实现类
 *
 * @author Kd
 * @since 2025-07-01
 */
@Service
@AllArgsConstructor
public class SettingServiceImpl implements SettingService {

	private final SettingMapper mapper;

	@Override
	public SettingEntity detail(String year) {
		LambdaQueryWrapper<SettingEntity> eq = Wrappers.<SettingEntity>lambdaQuery()
			.eq(SettingEntity::getYearAndMonth, year);
		SettingEntity detail = mapper.selectOne(eq);
		return detail;
	}

	@Override
	public boolean submit(SettingEntity dto) {
		if(Func.isAnyBlank(dto.getYearAndMonth())){
			throw new ServiceException("参数yearAndMonth不能为空");
		}
		boolean result;
		SettingEntity one = mapper.selectOne(Wrappers.<SettingEntity>lambdaQuery()
			.eq(SettingEntity::getYearAndMonth, dto.getYearAndMonth()));
		if (Func.isNull(one)) {
			//新增
			dto.setTenantId(SecureUtil.getTenantId());
			result = mapper.insert(dto) == 1;
		} else {
			//修改
			dto.setId(one.getId());
			result = mapper.updateById(dto) == 1;
		}
		return result;
	}

	@Override
	public Boolean fetchLastYearData(SettingEntity settingEntity) {
		if (Func.isBlank(settingEntity.getYearAndMonth())) {
			throw new ServiceException("参数yearAndMonth不能为空");
		}
		if(Func.isNull(settingEntity.getType())){
			throw new ServiceException("类型,读取上年度数据时需要传这个值，1：出勤时长，2：高企证书");
		}
		String lastYear = DateUtil.format(DateUtil.minusYears(DateUtil.parse(settingEntity.getYearAndMonth(), DateUtil.PATTERN_YYYY), 1), DateUtil.PATTERN_YYYY);
		LambdaQueryWrapper<SettingEntity> eq = Wrappers.<SettingEntity>lambdaQuery()
			.eq(SettingEntity::getYearAndMonth, lastYear);
		SettingEntity one = mapper.selectOne(eq);
		if (Func.isNull(one)) {
			throw new ServiceException("上年度没有数据");
		}
		one.setYearAndMonth(settingEntity.getYearAndMonth());

		SettingEntity dbEntity = mapper.selectOne(Wrappers.<SettingEntity>lambdaQuery()
			.eq(SettingEntity::getYearAndMonth, settingEntity.getYearAndMonth()));
		if(Func.isNull(dbEntity)){
			//新增
			SettingEntity insertObj = new SettingEntity();
			insertObj.setYearAndMonth(settingEntity.getYearAndMonth());
			insertObj.setTenantId(SecureUtil.getTenantId());
 			if(Func.equals(settingEntity.getType(), 1)){
				//出勤时长
				insertObj.setWorkHours(one.getWorkHours());
			}else if(Func.equals(settingEntity.getType(), 2)){
				//高企证书
				insertObj.setHighTechUrl(one.getHighTechUrl());
			}else {
				throw new ServiceException("参数type错误");
			}
			return mapper.insert(insertObj) == 1;
		}else {
			//修改
			if(Func.equals(settingEntity.getType(), 1)){
				dbEntity.setWorkHours(one.getWorkHours());
			}else if(Func.equals(settingEntity.getType(), 2)){
				dbEntity.setHighTechUrl(one.getHighTechUrl());
			}
			return mapper.updateById(dbEntity) == 1;
		}
	}

	@Override
	public List<SettingEntity> torchDevList(String yearMin, String yearMax) {
		return mapper.selectList(Wrappers.<SettingEntity>lambdaQuery()
				.between(SettingEntity::getYearAndMonth, yearMin, yearMax));
	}
}
