/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.business.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springblade.business.entity.FirstInformation;
import org.springblade.business.vo.FirstInformationVO;
import org.springblade.business.mapper.FirstInformationMapper;
import org.springblade.business.service.IFirstInformationService;
import org.springblade.common.utils.SnowFlakeUtil;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

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

/**
 * 首件信息记录表 服务实现类
 *
 * @author BladeX
 * @since 2022-06-20
 */
@Service
public class FirstInformationServiceImpl extends BaseServiceImpl<FirstInformationMapper, FirstInformation> implements IFirstInformationService {

	@Override
	public Boolean updateFirstInformation(Long id, String fileName, Integer reportNumber, Integer status) {
		if(StringUtils.isEmpty(fileName) && reportNumber == null && status == null){
			return null;
		} else {
			FirstInformation update = new FirstInformation();
			update.setId(id);
			if(StringUtils.isNotEmpty(fileName)){
				update.setFileName(fileName);
			}
			if(reportNumber != null){
				update.setReportNumber(reportNumber);
			}
			if(status != null){
				update.setStatus(status);
			}
			return this.updateById(update);
		}
	}

	@Override
	public Long saveFirstInformation(Long wbsNodeId, String fileName, Long dataId) {
		BladeUser bladeUser = AuthUtil.getUser();
		FirstInformation newData = new FirstInformation(fileName, wbsNodeId, null, 0, bladeUser.getUserId(), bladeUser.getUserName(), bladeUser.getDeptId().contains(",") ? Long.parseLong(bladeUser.getDeptId().split(",")[0]) : Long.parseLong(bladeUser.getDeptId()));
		newData.setId(SnowFlakeUtil.getId());
		//将主键返回，保存失败返回null
		return this.save(newData) ? newData.getId() : null;
	}

	@Override
	public IPage<FirstInformationVO> selectFirstInformationPage(IPage<FirstInformationVO> page, FirstInformationVO vo) {
		//处理分页相关
		Long current = (page.getCurrent() - 1L) * page.getSize();
		if(StringUtils.isNotEmpty(vo.getWbsNodeIds()) && !",".equals(vo.getWbsNodeIds())){
			List<String> wbsNodeArray = JSONArray.parseArray(JSONObject.toJSONString(vo.getWbsNodeIds().split(",")), String.class);
			wbsNodeArray.removeIf(StringUtils::isEmpty);
			vo.setWbsNodeArray(wbsNodeArray);
		}
		if(StringUtils.isNotEmpty(vo.getBetweenTime())){
			if(vo.getBetweenTime().contains("~")){
				String[] between = vo.getBetweenTime().split("~");
				vo.setStartTime(between[0].trim());
				vo.setEndTime(between[1].trim());
			} else {
				vo.setStartTime(vo.getBetweenTime().trim());
				vo.setEndTime(DateUtil.format(new Date(), "yyyy-MM-dd"));
			}
		}
		//获取列表总数
		int countValue = this.baseMapper.countFirstInformation(vo);
		//设置总数
		page.setTotal(countValue);
		return page.setRecords(this.baseMapper.selectFirstInformationPage(current, page.getSize(), vo));
	}

}
