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.PartnerServingStationDTO;
import com.prolog.rdc.tms.model.entity.line.TmsBasePartnerModel;
import com.prolog.rdc.tms.model.vo.fleetManagement.PartnerServingStationAddVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.PartnerServingStationCancelVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.PartnerServingStationPopVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.PartnerServingStationQueryVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.PartnerServingStationUpdateVO;
import com.prolog.rdc.tms.service.SysCodeengineService;
import com.prolog.rdc.tms.service.fleetManagement.PartnerServingStationService;

/**
 * 
 *
 * 车队管理-维修厂管理实现层
 *
 * @className :PartnerServingStationServiceImpl
 * @author :fanzw
 * @creationTime :2021/09/01
 * @description :
 * @modifier :fanzw
 * @modifyTime :2021/09/01
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@Service
public class PartnerServingStationServiceImpl implements PartnerServingStationService {

	@Autowired
	private TmsBasePartnerMapper tmsBasePartnerMapper;

	@Autowired
	private SysCodeengineService sysCodeengineService;

	/**
	 * 维修厂弹窗
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<PartnerServingStationDTO> listPartnerServingStationPopData(PartnerServingStationPopVO partnerServingStationPopVO) {
		ToolKits.isParamNull(partnerServingStationPopVO, "维修厂弹窗查询请求参数不为空");
		ToolKits.isParamNull(partnerServingStationPopVO.getPageNum(), "页码不为空");
		ToolKits.isParamNull(partnerServingStationPopVO.getPageSize(), "页码大小不为空");

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

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

		// 维修厂查询
		Criteria criteria = Criteria.forClass(TmsBasePartnerModel.class);
		Restriction r1 = ToolKits.isEmpty(partnerServingStationPopVO.getCode()) ? null : Restrictions.like("code", "%" + partnerServingStationPopVO.getCode() + "%");
		Restriction r2 = ToolKits.isEmpty(partnerServingStationPopVO.getName()) ? null : Restrictions.like("name", "%" + partnerServingStationPopVO.getName() + "%");
		Restriction r3 = ToolKits.isEmpty(partnerServingStationPopVO.getZjm()) ? null : Restrictions.like("zjm", "%" + partnerServingStationPopVO.getZjm() + "%");
		Restriction r4 = Restrictions.eq("status", StatusConstant.STATUS_ENABLE);
		Restriction r5 = Restrictions.eq("type", DataDictionaryConstant.PARTNER_TYPE_WXC);
		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<PartnerServingStationDTO> partnerServingStationDTOs = new ArrayList<PartnerServingStationDTO>();
		for (TmsBasePartnerModel tmsBasePartnerModel : tmsBasePartnerModels) {
			// 响应对象
			PartnerServingStationDTO partnerServingStationDTO = new PartnerServingStationDTO();
			BeanUtils.copyProperties(tmsBasePartnerModel, partnerServingStationDTO);

			// 添加响应对象
			partnerServingStationDTOs.add(partnerServingStationDTO);
		}
		// 重组分页属性，保证DTO分页正确
		Page<PartnerServingStationDTO> page = PageUtils.getPage(partnerServingStationDTOs);
		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<PartnerServingStationDTO> listPartnerServingStationData(PartnerServingStationQueryVO partnerServingStationQueryVO) {
		ToolKits.isParamNull(partnerServingStationQueryVO, "维修厂查询请求参数不为空");
		ToolKits.isParamNull(partnerServingStationQueryVO.getPageNum(), "页码不为空");
		ToolKits.isParamNull(partnerServingStationQueryVO.getPageSize(), "页码大小不为空");

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

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

		// 维修厂录入开始时间
		Date startdate = partnerServingStationQueryVO.getStartDate();
		// 维修厂录入结束时间
		Date enddate = partnerServingStationQueryVO.getEndDate();

		// 维修厂查询
		Criteria criteria = Criteria.forClass(TmsBasePartnerModel.class);
		Restriction r1 = ToolKits.isEmpty(partnerServingStationQueryVO.getZjm()) ? null : Restrictions.like("zjm", "%" + partnerServingStationQueryVO.getZjm() + "%");
		Restriction r2 = ToolKits.isEmpty(partnerServingStationQueryVO.getCode()) ? null : Restrictions.like("code", "%" + partnerServingStationQueryVO.getCode() + "%");
		Restriction r3 = ToolKits.isEmpty(partnerServingStationQueryVO.getName()) ? null : Restrictions.like("name", "%" + partnerServingStationQueryVO.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_WXC);
		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<PartnerServingStationDTO> partnerServingStationDTOs = new ArrayList<PartnerServingStationDTO>();
		for (TmsBasePartnerModel tmsBasePartnerModel : tmsBasePartnerModels) {
			// 响应对象
			PartnerServingStationDTO partnerServingStationDTO = new PartnerServingStationDTO();
			BeanUtils.copyProperties(tmsBasePartnerModel, partnerServingStationDTO);

			// 添加响应对象
			partnerServingStationDTOs.add(partnerServingStationDTO);
		}
		// 重组分页属性，保证DTO分页正确
		Page<PartnerServingStationDTO> page = PageUtils.getPage(partnerServingStationDTOs);
		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 addPartnerServingStationData(PartnerServingStationAddVO partnerServingStationAddVO) {
		ToolKits.isParamNull(partnerServingStationAddVO, "维修厂新增请求参数不为空");
		ToolKits.isParamNull(partnerServingStationAddVO.getCode(), "维修厂编号不为空");
		ToolKits.isParamNull(partnerServingStationAddVO.getName(), "维修厂名称不为空");
		ToolKits.isParamNull(partnerServingStationAddVO.getBranchId(), "配送中心ID不为空");
		ToolKits.isParamNull(partnerServingStationAddVO.getBranchName(), "配送中心名称不为空");
		ToolKits.isParamNull(partnerServingStationAddVO.getAddress(), "地址不为空");

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

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

		Criteria criteria = Criteria.forClass(TmsBasePartnerModel.class);
		Restriction r1 = Restrictions.eq("code", partnerServingStationAddVO.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(partnerServingStationAddVO, tmsBasePartnerModel);
			tmsBasePartnerModel.setId(Long.valueOf(id));
			tmsBasePartnerModel.setType(DataDictionaryConstant.PARTNER_TYPE_WXC);
			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("维修厂编号【" + partnerServingStationAddVO.getCode() + "】已存在");
		}
	}

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

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

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

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

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

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

	/**
	 * 维修厂作废
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void cancelPartnerServingStationData(List<PartnerServingStationCancelVO> partnerServingStationCancelVOs) {
		ToolKits.isParamNull(partnerServingStationCancelVOs, "维修厂作废请求参数不为空");

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

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

		for (PartnerServingStationCancelVO partnerServingStationCancelVO : partnerServingStationCancelVOs) {
			ToolKits.isParamNull(partnerServingStationCancelVO.getId(), "维修厂ID不为空");

			TmsBasePartnerModel tmsBasePartnerModel = tmsBasePartnerMapper.findById(partnerServingStationCancelVO.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【" + partnerServingStationCancelVO.getId() + "】不存在");
			}
		}
	}

}
