
package org.sky.scientific.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
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.Func;
import org.sky.core.tool.utils.StringUtil;
import org.sky.scientific.excel.XmWwxmCostExcel;
import org.sky.scientific.mapper.XmMapper;
import org.sky.scientific.mapper.XmWwxmCostMapper;
import org.sky.scientific.mapper.XmWwxmMapper;
import org.sky.scientific.pojo.dto.XmWwxmCostQuery;
import org.sky.scientific.pojo.entity.XmWwxmCost;
import org.sky.scientific.pojo.entity.XmWwxmEntity;
import org.sky.scientific.pojo.vo.XmWwxmCostVO;
import org.sky.scientific.service.IXmWwxmCostService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 委托外部研发费用-基本信息台账 服务实现类
 *
 * @author Kd
 * @since 2025-06-29
 */
@Service
@AllArgsConstructor
public class XmWwxmCostServiceImpl extends BaseServiceImpl<XmWwxmCostMapper, XmWwxmCost> implements IXmWwxmCostService {

	private final XmMapper xmMapper;
	private final XmWwxmMapper wwxmMapper;

	@Override
	public boolean submit(XmWwxmCost cost) {

		XmCostDetailsServiceImpl.judgeDate(cost.getPayDate(), cost.getYearAndMonth());
		boolean result = false;
		if (Func.isNull(cost.getId())) {
			result = this.save(cost);
		} else {
			result = this.updateById(cost);
		}
		return result;
	}

	@Override
	public void importWwxmCost(MultipartFile file, String yearAndMonth) {
		if (Func.isBlank(yearAndMonth)) {
			throw new ServiceException("参数yearAndMonth不能为空");
		}
		List<XmWwxmCostExcel> dataList = ExcelUtil.read(file, 0, 3, XmWwxmCostExcel.class);
		List<XmWwxmCost> insertList = new ArrayList<>();
		int batchCount = 20;
		for (XmWwxmCostExcel data : dataList) {
			XmCostDetailsServiceImpl.judgeDate(data.getPayDate(), yearAndMonth);

			if (StringUtil.isAllBlank(data.getXmmc(), data.getXmbh())) {
				throw new ServiceException("序号为[" + data.getSerialNumber() + "]的项目研发编号和项目研发名称都为空");
			}
			Long xmId = xmMapper.getXmIdByXmmcOrXmbh(yearAndMonth, data.getXmmc(), data.getXmbh());
			if (xmId == null) {
				throw new ServiceException("序号为[" + data.getSerialNumber() + "]的项目不存在");
			}

			LambdaQueryWrapper<XmWwxmEntity> eq = Wrappers.<XmWwxmEntity>lambdaQuery()
				.eq(XmWwxmEntity::getXmId, xmId)
				.eq(XmWwxmEntity::getYearAndMonth, yearAndMonth.substring(0, 4))
				.eq(XmWwxmEntity::getStfqc, data.getStfqc());
			XmWwxmEntity wwxm = wwxmMapper.selectOne(eq);
			if (Func.isNull(wwxm)) {
				throw new ServiceException("序号[" + data.getSerialNumber() + "]的数据有误");
			}

			XmWwxmCost cost = new XmWwxmCost();
			cost.setWwId(wwxm.getId());
			cost.setPayDate(data.getPayDate());
			cost.setPayAmount(data.getPayAmount());
			cost.setYearAndMonth(yearAndMonth);
			insertList.add(cost);

			if (insertList.size() % batchCount == 0) {
				if (this.saveBatch(insertList)) insertList.clear();
			}
		}
		if(!Func.isEmpty(insertList)){
			this.saveBatch(insertList);
		}
	}

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

	@Override
	public IPage<XmWwxmCostVO> selectPage(IPage<XmWwxmCostVO> page, XmWwxmCostQuery dto) {
		if (Func.isBlank(dto.getYearAndMonth())) {
			throw new ServiceException("参数yearAndMonth不能为空");
		}
		List<XmWwxmCostVO> list = null;
		if (dto.getYearAndMonth().length() == 6) {
			list = baseMapper.selectPageByYearAndMonth(page, dto);
		} else if (dto.getYearAndMonth().length() == 4) {
			list = baseMapper.selectPageByYear(page, dto);
		} else {
			throw new ServiceException("参数yearAndMonth格式错误，yyyyMM或yyyy");
		}
		return page.setRecords(list);
	}

	@Override
	public void export(HttpServletResponse response, XmWwxmCostQuery dto) {

	}
}
