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.GoodsBrandConvert;
import com.dk.mdm.infrastructure.convert.mst.GoodsSeriesConvert;
import com.dk.mdm.model.pojo.mst.GoodsBrand;
import com.dk.mdm.model.pojo.mst.GoodsSeries;
import com.dk.mdm.mapper.mst.GoodsSeriesMapper;
import com.dk.common.service.BaseService;
import com.dk.common.mapper.BaseMapper;
import com.dk.mdm.model.query.mst.GoodsBrandQuery;
import com.dk.mdm.model.query.mst.GoodsSeriesQuery;
import com.dk.mdm.model.response.mst.GoodsBrandResponse;
import com.dk.mdm.model.response.mst.GoodsSeriesResponse;
import com.dk.mdm.model.vo.mst.GoodsBrandVO;
import com.dk.mdm.model.vo.mst.GoodsSeriesVO;
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.List;
import java.util.Map;

@Service
@Transactional
public class GoodsSeriesService extends BaseService<GoodsSeries> {

	@Override
	public BaseMapper<GoodsSeries> getRepository() {
		return goodsSeriesMapper;
	}

	@Autowired
	private GoodsSeriesMapper goodsSeriesMapper;


	@Autowired
	private GoodsSeriesConvert goodsSeriesConvert;

	@Autowired
	private CommonService commonService;

	/**
	 * @desc : 重写主键
	 * @author : 于继渤
	 * @date : 2024/2/29 20:29
	 */
	@Override
	public String getPrimaryKey() {
		return "series_id";
	}



	/**
	 * @desc : 查询
	 * @author : 于继渤
	 * @date : 2023/1/5 9:39
	 */
	@Pagination
	public ResponseResultVO<PageList<GoodsSeriesResponse>> selectByCond(GoodsSeriesQuery goodsSeriesQuery) {
		return super.mergeListWithCount(goodsSeriesQuery, goodsSeriesMapper.selectByCond(goodsSeriesQuery),
				goodsSeriesMapper.countByCond(goodsSeriesQuery));
	}

	/**
	 * @desc : 查询
	 * @author : 王英杰
	 * @date : 2023/1/5 9:39
	 */
	@Pagination
	public ResponseResultVO<List<GoodsSeriesResponse>> listByNopage(GoodsSeriesQuery goodsSeriesQuery) {

			return  ResponseResultUtil.success(goodsSeriesMapper.listByNopage(goodsSeriesQuery)) ;
	}

	/**
	 * @desc : 新建
	 * @author : 于继渤
	 * @date : 2023/1/5 9:39
	 */
	@Transactional(
			rollbackFor = {Exception.class}
	)
	public ResponseResultVO<?> insert(GoodsSeriesVO goodsSeriesVO) {
		//实体转换
		GoodsSeries goodsSeries = goodsSeriesConvert.convertToPo(goodsSeriesVO);

		//设置序号
		goodsSeries.setDisplayNo(commonService.getMaxDisplayNo(Constant.DisplayNoTable.GOODSSERIES));
		//设置编码
		Map<String, Object> uniqueNoteCode = commonService.getUniqueNoteCode(Constant.docNameConstant.SERIES.getName(), true);
		goodsSeries.setSeriesId(uniqueNoteCode.get("outId").toString());
		goodsSeries.setSeriesCode(uniqueNoteCode.get("outNote").toString());
		//新建
		goodsSeriesMapper.insert(goodsSeries);
		return ResponseResultUtil.success();
	}



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

}
