
package org.sky.scientific.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.annotation.Resource;
import org.sky.core.excel.util.ExcelUtil;
import org.sky.core.log.exception.ServiceException;
import org.sky.core.mp.base.BaseServiceImpl;
import org.sky.core.tool.utils.*;
import org.sky.scientific.excel.XmDirectCostExcel;
import org.sky.scientific.mapper.XmDirectCostMapper;
import org.sky.scientific.mapper.XmMapper;
import org.sky.scientific.pojo.entity.XmDirectCostEntity;
import org.sky.scientific.service.IXmDirectCostService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * 研发项目直投费用清单 服务实现类
 *
 * @author Kd
 * @since 2025-07-17
 */
@Service
public class XmDirectCostServiceImpl extends BaseServiceImpl<XmDirectCostMapper, XmDirectCostEntity> implements IXmDirectCostService {

	@Resource
	private XmMapper xmMapper;

	@Override
	public IPage<XmDirectCostEntity> selectXmDirectCostPage(IPage<XmDirectCostEntity> page, XmDirectCostEntity dto) {
		if (StringUtil.isBlank(dto.getYearAndMonth())) {
			throw new ServiceException("参数yearAndMonth不能为空");
		}
		return page.setRecords(baseMapper.selectXmDirectCostPage(page, dto));
	}


	@Override
	public List<XmDirectCostExcel> exportXmDirectCost(Wrapper<XmDirectCostEntity> queryWrapper) {
		return baseMapper.exportXmDirectCost(queryWrapper);
	}

	@Override
	public boolean importXmDirectCost(MultipartFile file, String yearAndMonth) {
		if (StringUtil.isBlank(yearAndMonth)) {
			throw new ServiceException("参数yearAndMonth不能为空");
		}
		List<XmDirectCostExcel> data = ExcelUtil.read(file, 0, 2, XmDirectCostExcel.class);
		if (CollectionUtil.isEmpty(data)) {
			throw new ServiceException("未能从模板中读取数据，请检查模板格式是否正确");
		}
		List<XmDirectCostEntity> insertList = new ArrayList<>();
		for (XmDirectCostExcel excel : data) {
			judgeDate(excel.getUseDate(), yearAndMonth);

			XmDirectCostEntity entity = Objects.requireNonNull(BeanUtil.copyProperties(excel, XmDirectCostEntity.class));
			if (!StringUtil.isAllBlank(excel.getXmmc(), excel.getXmbh())) {
				//根据项目名称或编号查询xmId
				Long xmId = xmMapper.getXmIdByXmmcOrXmbh(yearAndMonth, excel.getXmmc(), excel.getXmbh());
				if (xmId == null) {
					throw new ServiceException("研发项目[" + (StringUtil.isNotBlank(excel.getXmmc()) ? excel.getXmmc() + "," : "") + (StringUtil.isNotBlank(excel.getXmbh()) ? excel.getXmbh() : "") + "]不存在，请检查模板数据");
				}
				entity.setXmId(xmId);

			}
			entity.setYearAndMonth(yearAndMonth);
			insertList.add(entity);
		}
		return this.saveBatch(insertList);
	}

	@Override
	public boolean physicalDeleteByIds(List<Long> ids) {
		return baseMapper.physicalDeleteByIds(ids) > 0;
	}

	@Override
	public boolean submit(XmDirectCostEntity xmDirectCost) {
		if (StringUtil.isBlank(xmDirectCost.getYearAndMonth())) {
			throw new ServiceException("参数yearAndMonth不能为空");
		}
		judgeDate(xmDirectCost.getUseDate(), xmDirectCost.getYearAndMonth());
		return this.saveOrUpdate(xmDirectCost);
	}

	@Override
	public List<JSONObject> tourufeiyonghuizongbiao(XmDirectCostEntity dto) {
		if(Func.isBlank(dto.getYearAndMonth())){
			throw new ServiceException("参数yearAndMonth不能为空");
		}
		List<JSONObject> list = null;
		if(dto.getYearAndMonth().length() == 6){
			list = baseMapper.tourufeiyonghuizongbiao(dto);
		}else if(dto.getYearAndMonth().length() == 4){
			list = baseMapper.tourufeiyonghuizongbiaoByYear(dto);
		}
		return list;
	}

	private void judgeDate(Date date, String yearAndMonth) {
		//租赁日期，必须时当月日期
		if (date != null) {
			String format = DateUtil.format(date, DateUtil.PATTERN_YYYYMM);
			boolean equals = StringUtil.equals(format, yearAndMonth);
			if (!equals) {
				throw new ServiceException("日期" + format + "必须是" + yearAndMonth + "月的一天");
			}
		}
	}
}
