package com.dk.mdm.service.mst;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dk.common.infrastructure.constant.Constant;
import com.dk.common.infrastructure.enums.ErrorCodeEnum;
import com.dk.common.model.pojo.PageList;
import com.dk.common.response.ResponseCodeEnum;
import com.dk.common.response.ResponseResultUtil;
import com.dk.common.response.ResponseResultVO;
import com.dk.mdm.model.pojo.mst.PrintLayout;
import com.dk.mdm.mapper.mst.PrintLayoutMapper;
import com.dk.common.service.BaseService;
import com.dk.common.mapper.BaseMapper;
import com.dk.mdm.model.pojo.mst.Role;
import com.dk.mdm.model.vo.mst.RoleVo;
import com.dk.mdm.service.common.CommonService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class PrintLayoutService extends BaseService<PrintLayout> {

	@Override
	public String getPrimaryKey() {
		return "layout_id";
	}

	@Override
	public BaseMapper<PrintLayout> getRepository() {
		return printLayoutMapper;
	}

	@Autowired
	private PrintLayoutMapper printLayoutMapper;

	@Autowired
	private CommonService commonService;

	/**
	 * @desc : 获取打印数据
	 * @author : 周兴
	 * @date : 2024/4/22 15:28
	 */
	public ResponseResultVO<?> getPrintData(Map<String, Object> param) {
		Map<String, Object> map = printLayoutMapper.getPrintData(param);
		JSONObject obj = JSONObject.parseObject(map.get("f_get_print_data").toString());
		return ResponseResultUtil.success(obj.get("data"));
	}

	/**
	 * @desc   : 新建票据
	 * @author : 周兴
	 * @date   : 2024/4/23 8:38
	 */
	@Transactional(
			rollbackFor = {Exception.class}
	)
	public ResponseResultVO<?> insert(PrintLayout printLayout) {
		// 判断票据名称是否重复
		Long count = printLayoutMapper.selectByName(printLayout);
		if(count > 0){
			return ResponseResultUtil.error(ErrorCodeEnum.LAYOUT_NAME_EXISTS.getCode(),
					ErrorCodeEnum.LAYOUT_NAME_EXISTS.getMessage());
		}
		// 获取显示顺序
		Integer displayNo =commonService.getMaxDisplayNo(Constant.DisplayNoTable.PRINT_LAYOUT);
		printLayout.setDisplayNo(displayNo);
		//新建
		super.insert(printLayout);
		return ResponseResultUtil.success();
	}

	/**
	 * @desc   : 更新票据
	 * @author : 周兴
	 * @date   : 2024/4/23 8:38
	 */
	@Transactional(
			rollbackFor = {Exception.class}
	)
	public ResponseResultVO<Boolean> update(PrintLayout printLayout) {
		// 判断票据名称是否重复
		Long count = printLayoutMapper.selectByName(printLayout);
		if(count > 0){
			return ResponseResultUtil.error(ErrorCodeEnum.LAYOUT_NAME_EXISTS.getCode(),
					ErrorCodeEnum.LAYOUT_NAME_EXISTS.getMessage());
		}
		// 更新
		super.updateByUuid(printLayout);
		return ResponseResultUtil.success();
	}

}
