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

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

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

import com.prolog.common.util.ToolKits;
import com.prolog.framework.core.exception.ParameterException;
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.TmsBasePartnerMapper;
import com.prolog.rdc.tms.model.dto.fleetManagement.PartnerGasStationDTO;
import com.prolog.rdc.tms.model.entity.line.TmsBasePartnerModel;
import com.prolog.rdc.tms.model.vo.fleetManagement.PartnerGasStationAddVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.PartnerGasStationCancelVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.PartnerGasStationPopVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.PartnerGasStationQueryVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.PartnerGasStationUpdateVO;
import com.prolog.rdc.tms.service.SysCodeengineService;
import com.prolog.rdc.tms.service.fleetManagement.PartnerGasStationService;

/**
 * 
 *
 * 车队管理-加油站管理实现层
 *
 * @className :PartnerGasStationServiceImpl
 * @author :fanzw
 * @creationTime :2021/09/01
 * @description :
 * @modifier :fanzw
 * @modifyTime :2021/09/01
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@Service
public class PartnerGasStationServiceImpl implements PartnerGasStationService {

	@Autowired
	private TmsBasePartnerMapper tmsBasePartnerMapper;

	@Autowired
	private SysCodeengineService sysCodeengineService;

	/**
	 * 加油站弹窗
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<PartnerGasStationDTO> listPartnerGasStationPopData(PartnerGasStationPopVO partnerGasStationPopVO) {
		ToolKits.isParamNull(partnerGasStationPopVO, "加油站弹窗查询请求参数不为空");
		ToolKits.isParamNull(partnerGasStationPopVO.getPageNum(), "页码不为空");
		ToolKits.isParamNull(partnerGasStationPopVO.getPageSize(), "页码大小不为空");

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

		// 分页
		PageUtils.startPage(partnerGasStationPopVO.getPageNum(), partnerGasStationPopVO.getPageSize());

		// 加油站查询
		Criteria criteria = Criteria.forClass(TmsBasePartnerModel.class);
		Restriction r1 = ToolKits.isEmpty(partnerGasStationPopVO.getCode()) ? null : Restrictions.like("code", "%" + partnerGasStationPopVO.getCode() + "%");
		Restriction r2 = ToolKits.isEmpty(partnerGasStationPopVO.getName()) ? null : Restrictions.like("name", "%" + partnerGasStationPopVO.getName() + "%");
		Restriction r3 = ToolKits.isEmpty(partnerGasStationPopVO.getZjm()) ? null : Restrictions.like("zjm", "%" + partnerGasStationPopVO.getZjm() + "%");
		Restriction r4 = Restrictions.eq("status", StatusConstant.STATUS_ENABLE);
		Restriction r5 = Restrictions.eq("type", DataDictionaryConstant.PARTNER_TYPE_JYZ);
		Restriction r6 = Restrictions.in("branchId", user.getWarehouses().toArray());
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4, r5, r6));
		criteria.setOrder(Order.newInstance().desc(new String[] { "createtime" }));
		// 执行查询
		List<TmsBasePartnerModel> tmsBasePartnerModels = tmsBasePartnerMapper.findByCriteria(criteria);

		// 响应对象集合
		List<PartnerGasStationDTO> partnerGasStationDTOs = new ArrayList<PartnerGasStationDTO>();
		for (TmsBasePartnerModel tmsBasePartnerModel : tmsBasePartnerModels) {
			// 响应对象
			PartnerGasStationDTO partnerGasStationDTO = new PartnerGasStationDTO();
			BeanUtils.copyProperties(tmsBasePartnerModel, partnerGasStationDTO);

			// 添加响应对象
			partnerGasStationDTOs.add(partnerGasStationDTO);
		}
		// 重组分页属性，保证DTO分页正确
		Page<PartnerGasStationDTO> page = PageUtils.getPage(partnerGasStationDTOs);
		if (tmsBasePartnerModels instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsBasePartnerModel> p0 = (com.github.pagehelper.Page<TmsBasePartnerModel>) tmsBasePartnerModels;
			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;
	}

	/**
	 * 加油站查询
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<PartnerGasStationDTO> listPartnerGasStationData(PartnerGasStationQueryVO partnerGasStationQueryVO) {
		ToolKits.isParamNull(partnerGasStationQueryVO, "加油站查询请求参数不为空");
		ToolKits.isParamNull(partnerGasStationQueryVO.getPageNum(), "页码不为空");
		ToolKits.isParamNull(partnerGasStationQueryVO.getPageSize(), "页码大小不为空");

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

		// 分页
		PageUtils.startPage(partnerGasStationQueryVO.getPageNum(), partnerGasStationQueryVO.getPageSize());

		// 加油站录入开始时间
		Date startdate = partnerGasStationQueryVO.getStartDate();
		// 加油站录入结束时间
		Date enddate = partnerGasStationQueryVO.getEndDate();

		// 加油站查询
		Criteria criteria = Criteria.forClass(TmsBasePartnerModel.class);
		Restriction r1 = ToolKits.isEmpty(partnerGasStationQueryVO.getZjm()) ? null : Restrictions.like("zjm", "%" + partnerGasStationQueryVO.getZjm() + "%");
		Restriction r2 = ToolKits.isEmpty(partnerGasStationQueryVO.getCode()) ? null : Restrictions.like("code", "%" + partnerGasStationQueryVO.getCode() + "%");
		Restriction r3 = ToolKits.isEmpty(partnerGasStationQueryVO.getName()) ? null : Restrictions.like("name", "%" + partnerGasStationQueryVO.getName() + "%");
		Restriction r4 = null;
		if (ToolKits.isNotEmpty(startdate) && ToolKits.isEmpty(enddate)) {
			r4 = Restrictions.between("createtime", startdate, new GregorianCalendar(3000, 1, 1).getTime());
		} else if (ToolKits.isEmpty(startdate) && ToolKits.isNotEmpty(enddate)) {
			r4 = Restrictions.between("createtime", new GregorianCalendar(1700, 1, 1).getTime(), enddate);
		} else if (ToolKits.isNotEmpty(startdate) && ToolKits.isNotEmpty(enddate)) {
			r4 = Restrictions.between("createtime", startdate, enddate);
		}
		Restriction r5 = Restrictions.eq("status", StatusConstant.STATUS_ENABLE);
		Restriction r6 = Restrictions.eq("type", DataDictionaryConstant.PARTNER_TYPE_JYZ);
		Restriction r7 = Restrictions.in("branchId", user.getWarehouses().toArray());
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4, r5, r6, r7));
		criteria.setOrder(Order.newInstance().desc(new String[] { "createtime" }));
		// 执行查询
		List<TmsBasePartnerModel> tmsBasePartnerModels = tmsBasePartnerMapper.findByCriteria(criteria);

		// 响应对象集合
		List<PartnerGasStationDTO> partnerGasStationDTOs = new ArrayList<PartnerGasStationDTO>();
		for (TmsBasePartnerModel tmsBasePartnerModel : tmsBasePartnerModels) {
			// 响应对象
			PartnerGasStationDTO partnerGasStationDTO = new PartnerGasStationDTO();
			BeanUtils.copyProperties(tmsBasePartnerModel, partnerGasStationDTO);

			// 添加响应对象
			partnerGasStationDTOs.add(partnerGasStationDTO);
		}
		// 重组分页属性，保证DTO分页正确
		Page<PartnerGasStationDTO> page = PageUtils.getPage(partnerGasStationDTOs);
		if (tmsBasePartnerModels instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsBasePartnerModel> p0 = (com.github.pagehelper.Page<TmsBasePartnerModel>) tmsBasePartnerModels;
			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;
	}

	/**
	 * 加油站新增
	 */
	@Override
	public void addPartnerGasStationData(PartnerGasStationAddVO partnerGasStationAddVO) {
		ToolKits.isParamNull(partnerGasStationAddVO, "加油站新增请求参数不为空");
		ToolKits.isParamNull(partnerGasStationAddVO.getCode(), "加油站编号不为空");
		ToolKits.isParamNull(partnerGasStationAddVO.getName(), "加油站名称不为空");
		ToolKits.isParamNull(partnerGasStationAddVO.getBranchId(), "配送中心ID不为空");
		ToolKits.isParamNull(partnerGasStationAddVO.getBranchName(), "配送中心名称不为空");
		ToolKits.isParamNull(partnerGasStationAddVO.getAddress(), "地址不为空");

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

		// 当前时间
		Date date = new Date();

		Criteria criteria = Criteria.forClass(TmsBasePartnerModel.class);
		Restriction r1 = Restrictions.eq("code", partnerGasStationAddVO.getCode());
		criteria.setRestriction(Restrictions.and(r1));
		List<TmsBasePartnerModel> tmsBasePartnerModels = tmsBasePartnerMapper.findByCriteria(criteria);
		if (ToolKits.isEmpty(tmsBasePartnerModels)) {
			// 获取加油站ID
			String id = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.PARTNER_GS_ID, "L");
			if (ToolKits.isEmpty(id)) {
				throw new ParameterException("加油站ID获取失败");
			}

			TmsBasePartnerModel tmsBasePartnerModel = new TmsBasePartnerModel();
			BeanUtils.copyProperties(partnerGasStationAddVO, tmsBasePartnerModel);
			tmsBasePartnerModel.setId(Long.valueOf(id));
			tmsBasePartnerModel.setType(DataDictionaryConstant.PARTNER_TYPE_JYZ);
			tmsBasePartnerModel.setStatus(StatusConstant.STATUS_ENABLE);
			tmsBasePartnerModel.setSource(DataDictionaryConstant.SOURCE_ADD);
			tmsBasePartnerModel.setEnterpriseId(user.getEnterpriseId());
			tmsBasePartnerModel.setEnterpriseName(user.getEnterpriseName());
			tmsBasePartnerModel.setCreateperson(user.getNickname());
			tmsBasePartnerModel.setCreatetime(date);
			tmsBasePartnerMapper.save(tmsBasePartnerModel);
		} else {
			throw new ParameterException("加油站编号【" + partnerGasStationAddVO.getCode() + "】已存在");
		}
	}

	/**
	 * 加油站修改
	 * 
	 * @param partnerGasStationUpdateVO
	 */
	@Override
	public void updatePartnerGasStationData(PartnerGasStationUpdateVO partnerGasStationUpdateVO) {
		ToolKits.isParamNull(partnerGasStationUpdateVO, "加油站修改请求参数不为空");
		ToolKits.isParamNull(partnerGasStationUpdateVO.getId(), "加油站ID不为空");
		ToolKits.isParamNull(partnerGasStationUpdateVO.getCode(), "加油站编号不为空");
		ToolKits.isParamNull(partnerGasStationUpdateVO.getName(), "加油站名称不为空");
		ToolKits.isParamNull(partnerGasStationUpdateVO.getType(), "类型不为空");
		ToolKits.isParamNull(partnerGasStationUpdateVO.getBranchId(), "配送中心ID不为空");
		ToolKits.isParamNull(partnerGasStationUpdateVO.getBranchName(), "配送中心名称不为空");
		ToolKits.isParamNull(partnerGasStationUpdateVO.getAddress(), "地址不为空");

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

		// 当前时间
		Date date = new Date();

		// 判断加油站编号是否修改重复
		Criteria criteria = Criteria.forClass(TmsBasePartnerModel.class);
		Restriction r1 = Restrictions.eq("code", partnerGasStationUpdateVO.getCode());
		criteria.setRestriction(Restrictions.and(r1));
		List<TmsBasePartnerModel> tmsBasePartnerModels = tmsBasePartnerMapper.findByCriteria(criteria);
		if (ToolKits.isNotEmpty(tmsBasePartnerModels)) {
			if (tmsBasePartnerModels.size() > 1) {
				throw new ParameterException("存在加油站编号重复的数据");
			}

			if (!partnerGasStationUpdateVO.getId().equals(tmsBasePartnerModels.get(0).getId())) {
				throw new ParameterException("加油站编号【" + partnerGasStationUpdateVO.getCode() + "】已存在,不能修改");
			}
		}

		TmsBasePartnerModel tmsBasePartnerModel = tmsBasePartnerMapper.findById(partnerGasStationUpdateVO.getId(), TmsBasePartnerModel.class);
		if (ToolKits.isNotEmpty(tmsBasePartnerModel)) {
			BeanUtils.copyProperties(partnerGasStationUpdateVO, tmsBasePartnerModel);
			tmsBasePartnerModel.setUpdateperson(user.getNickname());
			tmsBasePartnerModel.setUpdatetime(date);
			tmsBasePartnerMapper.update(tmsBasePartnerModel);
		} else {
			throw new ParameterException("加油站ID【" + partnerGasStationUpdateVO.getId() + "】不存在");
		}
	}

	/**
	 * 加油站作废
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void cancelPartnerGasStationData(List<PartnerGasStationCancelVO> partnerGasStationCancelVOs) {
		ToolKits.isParamNull(partnerGasStationCancelVOs, "加油站作废请求参数不为空");

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

		// 当前时间
		Date date = new Date();

		for (PartnerGasStationCancelVO partnerGasStationCancelVO : partnerGasStationCancelVOs) {
			ToolKits.isParamNull(partnerGasStationCancelVO.getId(), "加油站ID不为空");

			TmsBasePartnerModel tmsBasePartnerModel = tmsBasePartnerMapper.findById(partnerGasStationCancelVO.getId(), TmsBasePartnerModel.class);
			if (ToolKits.isNotEmpty(tmsBasePartnerModel)) {
				tmsBasePartnerModel.setStatus(StatusConstant.STATUS_DISABLE);
				tmsBasePartnerModel.setUpdateperson(user.getNickname());
				tmsBasePartnerModel.setUpdatetime(date);
				tmsBasePartnerMapper.update(tmsBasePartnerModel);
			} else {
				throw new ParameterException("加油站ID【" + tmsBasePartnerModel.getId() + "】不存在");
			}
		}
	}

}
