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

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

import org.apache.ibatis.plugin.PluginException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.prolog.framework.core.pojo.Page;
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.dao.mapper.PrinterRouteMapper;
import com.prolog.rdc.tms.model.dto.print.PrinterRouteDTO;
import com.prolog.rdc.tms.model.entity.line.PrinterRoute;
import com.prolog.rdc.tms.model.vo.print.DeleteSysPrinterVO;
import com.prolog.rdc.tms.model.vo.print.PrinterRouteVO;
import com.prolog.rdc.tms.model.vo.print.QuerySysPrinterRouteVO;
import com.prolog.rdc.tms.service.SysRefdatadService;
import com.prolog.rdc.tms.service.print.PrinterRouteService;
import com.prolog.rdc.tms.util.PublicMethods;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;

/**
 * 
 *
 * 打印路由实现层
 *
 * @className :PrinterRouteServiceImpl
 * @author :fanzw
 * @creationTime :2022/11/21
 * @description :
 *
 * @modifier :fanzw
 * @modifyTime :2022/11/21
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@Service
public class PrinterRouteServiceImpl implements PrinterRouteService {

	private final Logger logger = LoggerFactory.getLogger(PrinterRouteServiceImpl.class);

	@Autowired
	private PrinterRouteMapper service;

	@Autowired
	private SysRefdatadService sysRefdatadService;

	/**
	 * @Decription 新增打印机配置
	 * @param vo 新增入参
	 */
	@Override
	public Long save(PrinterRouteVO vo) {
		logger.info("打印机配置新增入参：" + JSONObject.toJSONString(vo));
		// 1.重复校验,通过编码和名称查询是否有打印机信息
		UserEntity user = PlgFxUsrUtils.getUserEntity();

		List<PrinterRoute> modelList = service.findByCodeAndName(vo.getEmployeeId(), vo.getRouteName(), vo.getPrinterId(), null, user.getEnterpriseId());
		if (CollUtil.isNotEmpty(modelList) && modelList.size() > 0) {
			throw new PluginException("打印机编码已存在，不要重复添加");
		}
		DateTime currentDate = DateUtil.date();
		PrinterRoute model = new PrinterRoute();
		BeanUtils.copyProperties(vo, model);
		model.setEmployeeId(String.valueOf(user.getUserid()));
		model.setCreatedBy(user.getNickname());
		model.setCreatedDate(currentDate);
		model.setLastModifiedBy(user.getNickname());
		model.setLastModifiedDate(currentDate);
		model.setEnterpriseId(user.getEnterpriseId());
		model.setEnterpriseName(user.getEnterpriseName());
		service.save(model);

		return model.getId();
	}

	/**
	 * 修改打模版印机
	 * 
	 * @param vo 修改参数
	 */
	@Override
	public void update(PrinterRouteVO vo) {
		Assert.notNull(vo.getId(), "打印机模版ID不能为空");
		// 1.重复校验,通过编码和名称查询是否有打印机信息
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		List<PrinterRoute> modelList = service.findByCodeAndName(vo.getEmployeeId(), vo.getRouteName(), vo.getPrinterId(), vo.getId(), user.getEnterpriseId());
		if (CollUtil.isNotEmpty(modelList) && modelList.size() > 0) {
			throw new PluginException("打印机模版编码已存在，不要重复添加");
		}
		PrinterRoute model = service.findByNativeId(vo.getId());
		Assert.notNull(model, "打印机模版信息为空");
		BeanUtils.copyProperties(vo, model);
		// 更新操作
		model.setLastModifiedBy(user.getNickname());
		model.setLastModifiedDate(DateUtil.date());
		service.update(model);
	}

	/**
	 * 打印机列表查询
	 * 
	 * @param vo 查询条件
	 * @return page
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<PrinterRouteDTO> findList(QuerySysPrinterRouteVO vo) throws Exception {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
		List<PrinterRoute> list = service.findList(vo, user.getEnterpriseId());

		List<Map<String, Object>> list1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.SYS_PRINTER_CONFIGURATION_STATUS, null, null, null);

		List<PrinterRouteDTO> dtoList = Lists.newArrayList();
		for (PrinterRoute model : list) {
			PrinterRouteDTO dto = new PrinterRouteDTO();
			BeanUtils.copyProperties(model, dto);
			dto.setStatusName(PublicMethods.change(list1, model.getStatus()));

			dtoList.add(dto);
		}
		// 分页设置
		Page<PrinterRouteDTO> page = PageUtils.getPage(dtoList);
		if (list instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<PrinterRoute> p0 = (com.github.pagehelper.Page<PrinterRoute>) list;
			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 delete(DeleteSysPrinterVO vo) {
		logger.info("删除入参:" + JSONObject.toJSONString(vo));

		UserEntity user = PlgFxUsrUtils.getUserEntity();

		service.delete(vo.getIds(), DateUtil.date(), user.getNickname());
	}

	// 获取详情
	@Override
	public PrinterRouteDTO detail(Long id) throws Exception {
		PrinterRoute model = service.findByNativeId(id);
		Assert.notNull(model, "查询路由详情为空");
		PrinterRouteDTO dto = new PrinterRouteDTO();
		BeanUtils.copyProperties(model, dto);

		List<Map<String, Object>> list1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.SYS_PRINTER_CONFIGURATION_STATUS, null, null, null);

		dto.setStatusName(PublicMethods.change(list1, model.getStatus()));

		return dto;
	}

	@Override
	public List<PrinterRoute> findByEmployeeId(String employeeId, Long warehouseId) {
		return service.findByEmployeeId(employeeId, warehouseId);
	}
}