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.PrinterTemplateMapper;
import com.prolog.rdc.tms.model.dto.print.PrinterTemplateDTO;
import com.prolog.rdc.tms.model.entity.line.PrinterTemplate;
import com.prolog.rdc.tms.model.vo.print.DeleteSysPrinterVO;
import com.prolog.rdc.tms.model.vo.print.PrinterTemplateVO;
import com.prolog.rdc.tms.model.vo.print.QuerySysPrinterTemplateVO;
import com.prolog.rdc.tms.service.SysRefdatadService;
import com.prolog.rdc.tms.service.print.PrinterTemplateService;
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 :PrinterTemplateServiceImpl
 * @author :fanzw
 * @creationTime :2022/11/21
 * @description :
 *
 * @modifier :fanzw
 * @modifyTime :2022/11/21
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@Service
public class PrinterTemplateServiceImpl implements PrinterTemplateService {

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

	@Autowired
	private PrinterTemplateMapper service;

	@Autowired
	private SysRefdatadService sysRefdatadService;

	/**
	 * @Decription 新增打印机配置
	 * @param vo 新增入参
	 */
	@Override
	public void save(PrinterTemplateVO vo) {
		logger.info("打印机配置新增入参：" + JSONObject.toJSONString(vo));
		// 1.重复校验,通过编码和名称查询是否有打印机信息
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		List<PrinterTemplate> modelList = service.findByCodeAndName(vo.getPlanCode(), vo.getPlanName(), null, user.getEnterpriseId(), vo.getCompanyType(), vo.getCompanyName(), vo.getWarehouseId(), vo.getTemplateType());
		if (CollUtil.isNotEmpty(modelList) && modelList.size() > 0) {
			throw new PluginException("打印方案编码已存在，不要重复添加");
		}
		DateTime currentDate = DateUtil.date();
		PrinterTemplate model = new PrinterTemplate();
		BeanUtils.copyProperties(vo, model);
		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);
	}

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

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

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

		List<PrinterTemplateDTO> dtoList = Lists.newArrayList();
		for (PrinterTemplate model : list) {
			PrinterTemplateDTO dto = new PrinterTemplateDTO();
			BeanUtils.copyProperties(model, dto);

			dto.setStatusName(PublicMethods.change(list1, model.getStatus()));
			dto.setTemplateTypeName(PublicMethods.change(list2, model.getTemplateType()));
			dto.setCompanyTypeName(PublicMethods.change(list3, model.getCompanyType()));

			dtoList.add(dto);
		}

		// 分页设置
		Page<PrinterTemplateDTO> page = PageUtils.getPage(dtoList);
		if (list instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<PrinterTemplate> p0 = (com.github.pagehelper.Page<PrinterTemplate>) 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) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		logger.info("删除入参:" + JSONObject.toJSONString(vo));
		service.delete(vo.getIds(), DateUtil.date(), user.getNickname());
	}

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

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

		dto.setStatusName(PublicMethods.change(list1, model.getStatus()));
		dto.setTemplateTypeName(PublicMethods.change(list2, model.getTemplateType()));
		dto.setCompanyTypeName(PublicMethods.change(list3, model.getCompanyType()));

		return dto;
	}

	@Override
	public List<PrinterTemplate> findListByCode(String templateCode, Long warehouseId, String consignorId) {
		return service.findListByCode(templateCode, warehouseId, consignorId);
	}
}