package com.prolog.rdc.tms.service.basicConfigure.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.google.common.collect.Maps;
import com.prolog.common.util.ToolKits;
import com.prolog.framework.core.exception.PrologException;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.core.restriction.Restriction;
import com.prolog.framework.core.restriction.Restrictions;
import com.prolog.product.core.framework.entity.UserEntity;
import com.prolog.product.core.framework.util.PlgFxUsrUtils;
import com.prolog.rdc.tms.constant.SeriaNnumberConstant;
import com.prolog.rdc.tms.dao.mapper.SysParamConfigMapper;
import com.prolog.rdc.tms.model.dto.basicConfigure.ParamConfigQueryDTO;
import com.prolog.rdc.tms.model.entity.SysParamConfigModel;
import com.prolog.rdc.tms.model.vo.basicConfigure.ParamConfigSaveVO;
import com.prolog.rdc.tms.service.SysCodeengineService;
import com.prolog.rdc.tms.service.basicConfigure.SysParamConfigService;

import cn.hutool.core.date.DateUtil;

/**
 * 
 *
 * 系统参数配置实现层
 *
 * @className :SysParamConfigServiceImpl
 * @author :fanzw
 * @creationTime :2022/07/18
 * @description :
 *
 * @modifier :fanzw
 * @modifyTime :2022/07/18
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@Service
public class SysParamConfigServiceImpl implements SysParamConfigService {

	@Autowired
	private SysParamConfigMapper sysParamConfigMapper;

	@Autowired
	private SysCodeengineService sysCodeengineService;

	/**
	 * 系统参数查询
	 */
	@Override
	public List<ParamConfigQueryDTO> list() {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		// 按企业查询
		Criteria criteria = Criteria.forClass(SysParamConfigModel.class);
		Restriction r1 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1));
		List<SysParamConfigModel> sysParamConfigModels = sysParamConfigMapper.findByCriteria(criteria);

		// 响应对象集合
		List<ParamConfigQueryDTO> paramConfigQueryDTOs = new ArrayList<ParamConfigQueryDTO>();
		for (SysParamConfigModel sysParamConfigModel : sysParamConfigModels) {
			// 响应对象
			ParamConfigQueryDTO paramConfigQueryDTO = new ParamConfigQueryDTO();
			paramConfigQueryDTO.setCode(sysParamConfigModel.getCode());
			paramConfigQueryDTO.setName(sysParamConfigModel.getName());
			paramConfigQueryDTO.setValue(sysParamConfigModel.getValue());

			paramConfigQueryDTOs.add(paramConfigQueryDTO);
		}

		return paramConfigQueryDTOs;
	}

	/**
	 * 系统参数保存
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void save(List<ParamConfigSaveVO> paramConfigSaveVOs) {
		ToolKits.isParamNull(paramConfigSaveVOs, "系统参数保存请求不为空");

		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		for (ParamConfigSaveVO paramConfigSaveVO : paramConfigSaveVOs) {
			ToolKits.isParamNull(paramConfigSaveVO.getCode(), "系统参数编码不为空");
			ToolKits.isParamNull(paramConfigSaveVO.getName(), "系统参数名称不为空");
			ToolKits.isParamNull(paramConfigSaveVO.getValue(), "系统参数值不为空");

			// 当前时间
			Date date = DateUtil.date();

			Map<String, Object> queryMap = Maps.newHashMap();
			queryMap.put("enterpriseId", user.getEnterpriseId());
			queryMap.put("code", paramConfigSaveVO.getCode());
			List<SysParamConfigModel> sysParamConfigModels = sysParamConfigMapper.findByMap(queryMap, SysParamConfigModel.class);
			if (ToolKits.isEmpty(sysParamConfigModels)) {
				// 系统ID
				String id = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_SYS_PARAM_CONFIG_ID, "L");
				ToolKits.isParamNull(id, "系统ID生成失败");

				SysParamConfigModel sysParamConfigModel = new SysParamConfigModel();
				sysParamConfigModel.setId(Long.valueOf(id));
				sysParamConfigModel.setCode(paramConfigSaveVO.getCode());
				sysParamConfigModel.setName(paramConfigSaveVO.getName());
				sysParamConfigModel.setValue(paramConfigSaveVO.getValue());
				sysParamConfigModel.setEnterpriseId(user.getEnterpriseId());
				sysParamConfigModel.setEnterpriseName(user.getNickname());
				sysParamConfigModel.setCreateperson(user.getNickname());
				sysParamConfigModel.setCreatetime(date);
				sysParamConfigModel.setUpdateperson(user.getNickname());
				sysParamConfigModel.setUpdatetime(date);
				// 保存
				sysParamConfigMapper.save(sysParamConfigModel);
			} else {
				Assert.isTrue(sysParamConfigModels.size() == 1, "系统参数【" + paramConfigSaveVO.getCode() + "】维护重复");

				SysParamConfigModel sysParamConfigModel = sysParamConfigModels.get(0);
				sysParamConfigModel.setValue(paramConfigSaveVO.getValue());
				sysParamConfigModel.setUpdateperson(user.getNickname());
				sysParamConfigModel.setUpdatetime(date);
				sysParamConfigMapper.update(sysParamConfigModel);
			}
		}
	}

	/**
	 * 公共方法 通过参数编码查询单个参数对象
	 *
	 * @param
	 * @return
	 */
	@Override
	public SysParamConfigModel getValue(String code) throws Exception {
		ToolKits.isParamNull(code, "参数编码不能为空");

		UserEntity user = PlgFxUsrUtils.getUserEntity();
		Map<String, Object> queryMap = Maps.newHashMap();
		queryMap.put("enterpriseId", user.getEnterpriseId());
		queryMap.put("code", code);
		List<SysParamConfigModel> list = sysParamConfigMapper.findByMap(queryMap, SysParamConfigModel.class);
		if (ToolKits.isNotEmpty(list) && list.size() > 0) {
			return list.get(0);
		} else {
			throw new PrologException("没有查询到编码为【" + code + "】的参数配置");
		}
	}

	/**
	 * 定时任务 通过参数编码查询单个参数对象
	 *
	 * @param
	 * @return
	 */
	@Override
	public SysParamConfigModel getValueForTask(String code) throws Exception {
		Map<String, Object> queryMap = Maps.newHashMap();
		queryMap.put("code", code);
		List<SysParamConfigModel> list = sysParamConfigMapper.findByMap(queryMap, SysParamConfigModel.class);
		if (ToolKits.isNotEmpty(list) && list.size() > 0) {
			return list.get(0);
		} else {
			throw new PrologException("没有查询到编码为【" + code + "】的参数配置");
		}
	}

}
