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

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

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.prolog.common.util.DateUtils;
import com.prolog.common.util.ToolKits;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.core.restriction.Order;
import com.prolog.framework.core.restriction.Restriction;
import com.prolog.framework.core.restriction.Restrictions;
import com.prolog.framework.dao.util.PageUtils;
import com.prolog.product.core.framework.entity.UserEntity;
import com.prolog.product.core.framework.util.PlgFxUsrUtils;
import com.prolog.rdc.tms.constant.DataDictionaryConstant;
import com.prolog.rdc.tms.constant.SeriaNnumberConstant;
import com.prolog.rdc.tms.constant.StatusConstant;
import com.prolog.rdc.tms.dao.mapper.TmsBaseInsuranceMapper;
import com.prolog.rdc.tms.model.dto.fleetManagement.InsuranceDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.InsuranceViewDTO;
import com.prolog.rdc.tms.model.entity.line.TmsBaseInsuranceModel;
import com.prolog.rdc.tms.model.vo.fleetManagement.InsuranceAddVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.InsuranceDeleteVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.InsurancePopVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.InsuranceUpdateVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.InsuranceVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.InsuranceViewVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.InsuranceWhetherEnableVO;
import com.prolog.rdc.tms.service.SysCodeengineService;
import com.prolog.rdc.tms.service.SysRefdatadService;
import com.prolog.rdc.tms.service.fleetManagement.InsuranceService;
import com.prolog.rdc.tms.util.PublicMethods;

import cn.hutool.core.date.DateUtil;

/**
 * 
 *
 * 车队管理-保险资料实现层
 *
 * @className :InsuranceServiceImpl
 * @author :zsj
 * @creationTime :2022/04/18
 * @description :
 *
 * @modifier :zsj
 * @modifyTime :2022/04/18
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@Service
public class InsuranceServiceImpl implements InsuranceService {

	@Autowired
	private TmsBaseInsuranceMapper tmsBaseInsuranceMapper;

	@Autowired
	private SysRefdatadService sysRefdatadService;

	@Autowired
	private SysCodeengineService sysCodeengineService;

	/**
	 * 保险资料分页查询
	 * 
	 * @param vo
	 * @return
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<InsuranceDTO> query(InsuranceVO vo) {
		ToolKits.isParamNull(vo, "参数不能为空！");
		ToolKits.isParamNull(vo.getPageNum(), "当前页码不能为空");
		ToolKits.isParamNull(vo.getPageSize(), "每页条数不能为空");

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

		Criteria criteria = Criteria.forClass(TmsBaseInsuranceModel.class);
		Restriction r1 = ToolKits.isEmpty(vo.getCode()) ? null : Restrictions.like("code", "%" + vo.getCode() + "%");
		Restriction r2 = ToolKits.isEmpty(vo.getBeneficiaryType()) ? null : Restrictions.eq("beneficiaryType", vo.getBeneficiaryType());
		Restriction r3 = ToolKits.isEmpty(vo.getObjectName()) ? null : Restrictions.like("objectName", "%" + vo.getObjectName() + "%");
		Restriction r4 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4));
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));

		PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
		List<TmsBaseInsuranceModel> models = tmsBaseInsuranceMapper.findByCriteria(criteria);

		// 保险公司
		List<Map<String, Object>> restMessage1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.INSURANCE_COMPANY, null, null, null);
		// 保险类型
		List<Map<String, Object>> restMessage2 = sysRefdatadService.getDropDownById(DataDictionaryConstant.INSURANCE_TYPE, null, null, null);
		// 受益对象类型
		List<Map<String, Object>> restMessage3 = sysRefdatadService.getDropDownById(DataDictionaryConstant.BENEFICIARY_TYPE, null, null, null);
		// 设备状态
		List<Map<String, Object>> restMessage4 = sysRefdatadService.getDropDownById(DataDictionaryConstant.MAINDATA_STATUS, null, null, null);

		List<InsuranceDTO> dtos = Lists.newArrayList();
		for (TmsBaseInsuranceModel model : models) {
			InsuranceDTO dto = new InsuranceDTO();
			BeanUtils.copyProperties(model, dto);
			dto.setCompanyName(PublicMethods.change(restMessage1, String.valueOf(model.getCompany())));
			dto.setTypeName(PublicMethods.change(restMessage2, String.valueOf(model.getType())));
			dto.setBeneficiaryTypeName(PublicMethods.change(restMessage3, String.valueOf(model.getBeneficiaryType())));
			dto.setStatusName(PublicMethods.change(restMessage4, String.valueOf(model.getStatus())));
			dtos.add(dto);
		}

		Page<InsuranceDTO> page = PageUtils.getPage(dtos);
		if (models instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsBaseInsuranceModel> p0 = (com.github.pagehelper.Page<TmsBaseInsuranceModel>) models;
			page.setPageCount(p0.getPages());
			page.setPageNum(p0.getPageNum());
			page.setPageSize(p0.getPageSize());
			page.setTotalCount(p0.getTotal());
			page.setStartRow(p0.getStartRow());
			page.setEndRow(p0.getEndRow());
		}

		return page;
	}

	/**
	 * 保险资料弹窗
	 * 
	 * @param vo
	 * @return
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<InsuranceDTO> pop(InsurancePopVO vo) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		ToolKits.isParamNull(vo, "参数不能为空！");
		ToolKits.isParamNull(vo.getPageNum(), "当前页码不能为空");
		ToolKits.isParamNull(vo.getPageSize(), "每页条数不能为空");

		Criteria criteria = Criteria.forClass(TmsBaseInsuranceModel.class);
		Restriction r1 = ToolKits.isEmpty(vo.getCode()) ? null : Restrictions.like("code", "%" + vo.getCode() + "%");
		Restriction r2 = ToolKits.isEmpty(vo.getBeneficiaryType()) ? null : Restrictions.eq("beneficiaryType", vo.getBeneficiaryType());
		Restriction r3 = ToolKits.isEmpty(vo.getObjectName()) ? null : Restrictions.like("objectName", "%" + vo.getObjectName() + "%");
		Restriction r4 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4));
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));

		PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
		List<TmsBaseInsuranceModel> models = tmsBaseInsuranceMapper.findByCriteria(criteria);

		// 保险公司
		List<Map<String, Object>> restMessage1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.INSURANCE_COMPANY, null, null, null);
		// 保险类型
		List<Map<String, Object>> restMessage2 = sysRefdatadService.getDropDownById(DataDictionaryConstant.INSURANCE_TYPE, null, null, null);
		// 受益对象类型
		List<Map<String, Object>> restMessage3 = sysRefdatadService.getDropDownById(DataDictionaryConstant.BENEFICIARY_TYPE, null, null, null);
		// 设备状态
		List<Map<String, Object>> restMessage4 = sysRefdatadService.getDropDownById(DataDictionaryConstant.MAINDATA_STATUS, null, null, null);

		List<InsuranceDTO> dtos = Lists.newArrayList();
		for (TmsBaseInsuranceModel model : models) {
			InsuranceDTO dto = new InsuranceDTO();
			BeanUtils.copyProperties(model, dto);
			dto.setCompanyName(PublicMethods.change(restMessage1, String.valueOf(model.getCompany())));
			dto.setTypeName(PublicMethods.change(restMessage2, String.valueOf(model.getType())));
			dto.setBeneficiaryTypeName(PublicMethods.change(restMessage3, String.valueOf(model.getBeneficiaryType())));
			dto.setStatusName(PublicMethods.change(restMessage4, String.valueOf(model.getStatus())));
			dtos.add(dto);
		}

		Page<InsuranceDTO> page = PageUtils.getPage(dtos);
		if (models instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsBaseInsuranceModel> p0 = (com.github.pagehelper.Page<TmsBaseInsuranceModel>) models;
			page.setPageCount(p0.getPages());
			page.setPageNum(p0.getPageNum());
			page.setPageSize(p0.getPageSize());
			page.setTotalCount(p0.getTotal());
			page.setStartRow(p0.getStartRow());
			page.setEndRow(p0.getEndRow());
		}

		return page;
	}

	/**
	 * 保险资料查看
	 * 
	 * @param vo
	 * @return
	 */
	@Override
	public InsuranceViewDTO view(InsuranceViewVO vo) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		ToolKits.isParamNull(vo.getId(), "保险ID不能为空");

		TmsBaseInsuranceModel model = tmsBaseInsuranceMapper.findById(vo.getId(), TmsBaseInsuranceModel.class);
		ToolKits.isParamNull(model, "保险ID[" + vo.getId() + "]有误,查询为空!");

		// 保险公司
		List<Map<String, Object>> restMessage1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.INSURANCE_COMPANY, null, null, null);
		// 保险类型
		List<Map<String, Object>> restMessage2 = sysRefdatadService.getDropDownById(DataDictionaryConstant.INSURANCE_TYPE, null, null, null);
		// 受益对象类型
		List<Map<String, Object>> restMessage3 = sysRefdatadService.getDropDownById(DataDictionaryConstant.BENEFICIARY_TYPE, null, null, null);
		// 设备状态
		List<Map<String, Object>> restMessage4 = sysRefdatadService.getDropDownById(DataDictionaryConstant.MAINDATA_STATUS, null, null, null);

		InsuranceViewDTO dto = new InsuranceViewDTO();
		BeanUtils.copyProperties(model, dto);

		dto.setCompanyName(PublicMethods.change(restMessage1, String.valueOf(model.getCompany())));
		dto.setTypeName(PublicMethods.change(restMessage2, String.valueOf(model.getType())));
		dto.setBeneficiaryTypeName(PublicMethods.change(restMessage3, String.valueOf(model.getBeneficiaryType())));
		dto.setStatusName(PublicMethods.change(restMessage4, String.valueOf(model.getStatus())));
		return dto;
	}

	/**
	 * 保险资料新增
	 * 
	 * @param vo
	 */
	@Override
	public void save(InsuranceAddVO vo) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		ToolKits.isParamNull(vo.getCode(), "保险号不能为空");
		ToolKits.isParamNull(vo.getType(), "保险类型不能为空");
		ToolKits.isParamNull(vo.getCompany(), "保险公司不能为空");
		ToolKits.isParamNull(vo.getBeneficiaryType(), "受益对象类型不能为空");
		ToolKits.isParamNull(vo.getObjectId(), "对象ID不能为空");
		ToolKits.isParamNull(vo.getObjectCode(), "对象编号不能为空");
		ToolKits.isParamNull(vo.getObjectName(), "对象名称不能为空");
		ToolKits.isParamNull(vo.getStartTime(), "保险期限始不能为空");
		ToolKits.isParamNull(vo.getEndTime(), "保险期限止不能为空");

		// 判断保险单是否已存在
		PublicMethods.isThereAnyinformation(TmsBaseInsuranceModel.class, "code", vo.getCode(), "company", vo.getCompany().longValue(), "status", StatusConstant.STATUS_ENABLE, tmsBaseInsuranceMapper, vo.getCode() + "已存在!(同一保险公司的保单号不能重复)");
		// 判断保险单是否已存在未启用
		PublicMethods.isThereAnyinformation(TmsBaseInsuranceModel.class, "code", vo.getCode(), "company", vo.getCompany().longValue(), "status", StatusConstant.STATUS_DISABLE, tmsBaseInsuranceMapper, vo.getCode() + "已存在,请直接启用!");

		String id = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.INSURANCE_ID, "L");
		ToolKits.isParamNull(id, "获取保险ID失败！");

		Date date = DateUtil.date();

		TmsBaseInsuranceModel model = new TmsBaseInsuranceModel();
		BeanUtils.copyProperties(vo, model);
		model.setId(Long.valueOf(id));
		model.setStatus(StatusConstant.STATUS_ENABLE);
		model.setEnterpriseId(user.getEnterpriseId());
		model.setEnterpriseName(user.getEnterpriseName());
		model.setCreateperson(user.getNickname());
		model.setCreatetime(date);
		model.setUpdateperson(user.getNickname());
		model.setUpdatetime(date);

		tmsBaseInsuranceMapper.save(model);

	}

	/**
	 * 保险资料修改
	 * 
	 * @param vo
	 */
	@Override
	public void update(InsuranceUpdateVO vo) {
		ToolKits.isParamNull(vo.getType(), "保险类型不能为空");
		ToolKits.isParamNull(vo.getBeneficiaryType(), "受益对象类型不能为空");
		ToolKits.isParamNull(vo.getObjectId(), "对象ID不能为空");
		ToolKits.isParamNull(vo.getObjectCode(), "对象编号不能为空");
		ToolKits.isParamNull(vo.getObjectName(), "对象名称不能为空");
		ToolKits.isParamNull(vo.getStartTime(), "保险期限始不能为空");
		ToolKits.isParamNull(vo.getEndTime(), "保险期限止不能为空");

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

		TmsBaseInsuranceModel model = tmsBaseInsuranceMapper.findById(vo.getId(), TmsBaseInsuranceModel.class);
		ToolKits.isParamNull(model, "保险ID[" + vo.getId() + "]有误,查询为空!");

		if (model.getStatus() == StatusConstant.STATUS_DISABLE) {
			throw new RuntimeException("该保险已被停用不可修改");
		}

		BeanUtils.copyProperties(vo, model);

		model.setUpdateperson(user.getNickname());
		model.setUpdatetime(DateUtils.nowDate());

		tmsBaseInsuranceMapper.update(model);
	}

	/**
	 * 保险资料是否启用
	 * 
	 * @param vo
	 */
	@Override
	public void whetherEnable(InsuranceWhetherEnableVO insuranceWhetherEnableVO) {
		ToolKits.isParamNull(insuranceWhetherEnableVO, "保险资料是否启用请求不为空");
		ToolKits.isParamNull(insuranceWhetherEnableVO.getIds(), "保险ID集不能为空");
		ToolKits.isParamNull(insuranceWhetherEnableVO.getStatus(), "状态不能为空");

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

		Criteria criteria = Criteria.forClass(TmsBaseInsuranceModel.class);
		Restriction r1 = Restrictions.in("id", insuranceWhetherEnableVO.getIds().toArray());
		criteria.setRestriction(r1);

		Map<String, Object> map = Maps.newHashMap();
		map.put("status", insuranceWhetherEnableVO.getStatus());
		map.put("updateperson", user.getNickname());
		map.put("updatetime", DateUtil.date());

		tmsBaseInsuranceMapper.updateMapByCriteria(map, criteria);
	}

	/**
	 * 保险资料删除
	 * 
	 */
	@Override
	public void delete(InsuranceDeleteVO insuranceDeleteVO) {
		ToolKits.isParamNull(insuranceDeleteVO, "保险资料删除请求不为空");
		ToolKits.isParamNull(insuranceDeleteVO.getIds(), "保险ID集不能为空！");

		// 执行删除
		tmsBaseInsuranceMapper.deleteByIds(insuranceDeleteVO.getIds().toArray(), TmsBaseInsuranceModel.class);
	}
}
