package com.dk.mdm.service.mst;

import com.dk.common.infrastructure.annotaiton.Pagination;
import com.dk.common.infrastructure.constant.Constant;
import com.dk.common.model.pojo.PageList;
import com.dk.common.response.ResponseResultUtil;
import com.dk.common.response.ResponseResultVO;
import com.dk.mdm.infrastructure.convert.mst.UnitConvert;
import com.dk.mdm.model.pojo.mst.Unit;
import com.dk.mdm.mapper.mst.UnitMapper;
import com.dk.common.service.BaseService;
import com.dk.common.mapper.BaseMapper;
import com.dk.mdm.model.query.mst.UnitQuery;
import com.dk.mdm.model.response.mst.GoodsBrandResponse;
import com.dk.mdm.model.response.mst.UnitResponse;
import com.dk.mdm.model.vo.mst.UnitVO;
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.Map;

@Service
@Transactional
public class UnitService extends BaseService<Unit> {

	@Override
	public BaseMapper<Unit> getRepository() {
		return unitMapper;
	}

	@Autowired
	private UnitMapper unitMapper;

	@Autowired
	private UnitConvert unitConvert;

	@Autowired
	private CommonService commonService;
	/**
	 * @desc : 重写主键
	 * @author : 于继渤
	 * @date : 2024/2/29 20:29
	 */
	@Override
	public String getPrimaryKey() {
		return "unit_id";
	}
	/**
	 * @desc : 查询
	 * @author : 于继渤
	 * @date : 2023/1/5 9:39
	 */
	@Pagination
	public ResponseResultVO<PageList<UnitResponse>> selectByCond(UnitQuery UnitQuery) {
		return super.mergeListWithCount(UnitQuery, unitMapper.selectByCond(UnitQuery),
				unitMapper.countByCond(UnitQuery));
	}
	/**
	 * @desc : 新建
	 * @author : 于继渤
	 * @date : 2023/1/5 9:39
	 */
	@Transactional(
			rollbackFor = {Exception.class}
	)
	public ResponseResultVO<?> insert(UnitVO unitVO) {
		//实体转换
		Unit unit = unitConvert.convertToPo(unitVO);
		//设置序号
		unit.setDisplayNo(commonService.getMaxDisplayNo(Constant.DisplayNoTable.UNIT));
		//设置编码
		Map<String, Object> uniqueNoteCode = commonService.getUniqueNoteCode(Constant.docNameConstant.UNIT.getName(), true);
		unit.setUnitId(uniqueNoteCode.get("outId").toString());
		unit.setUnitCode(uniqueNoteCode.get("outNote").toString());
		//新建
		unitMapper.insert(unit);
		return ResponseResultUtil.success();
	}



	/**
	 * @desc : 编辑
	 * @author : 于继渤
	 * @date : 2023/1/5 9:39
	 */
	@Transactional(
			rollbackFor = {Exception.class}
	)
	public ResponseResultVO<?> update(UnitVO unitVO) {
		Unit unit = unitConvert.convertToPo(unitVO);
		super.updateByUuid(unit);
		return ResponseResultUtil.success();
	}
}
