package com.dk.mdm.service.ivt.inbound;

import com.dk.common.infrastructure.annotaiton.Pagination;
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.ResponseResultUtil;
import com.dk.common.response.ResponseResultVO;
import com.dk.mdm.infrastructure.convert.ivt.InboundConvert;
import com.dk.mdm.mapper.ivt.InboundItemMapper;
import com.dk.mdm.mapper.ivt.IntoReturnItemMapper;
import com.dk.mdm.model.pojo.ivt.Inbound;
import com.dk.mdm.mapper.ivt.InboundMapper;
import com.dk.common.service.BaseService;
import com.dk.common.mapper.BaseMapper;
import com.dk.mdm.model.pojo.mst.GoodsBrand;
import com.dk.mdm.model.query.ivt.InboundItemQuery;
import com.dk.mdm.model.query.ivt.InboundQuery;
import com.dk.mdm.model.query.pur.PurchaseItemQuery;
import com.dk.mdm.model.query.pur.PurchaseQuery;
import com.dk.mdm.model.query.sale.OrderItemQuery;
import com.dk.mdm.model.response.ivt.InboundItemResponse;
import com.dk.mdm.model.response.ivt.InboundResponse;
import com.dk.mdm.model.response.pur.PurchaseItemResponse;
import com.dk.mdm.model.response.pur.PurchaseResponse;
import com.dk.mdm.model.response.sale.OrderItemResponse;
import com.dk.mdm.model.response.sale.OrderResponse;
import com.dk.mdm.model.vo.ivt.InboundVO;
import com.dk.mdm.model.vo.mst.GoodsBrandVO;
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.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class InboundService extends BaseService<Inbound> {

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

	@Override
	public BaseMapper<Inbound> getRepository() {
		return inboundMapper;
	}

	@Autowired
	private InboundMapper inboundMapper;

	@Autowired
	private InboundItemMapper inboundItemMapper;

	@Autowired
	private InboundConvert inboundConvert;

	@Autowired
	private CommonService commonService;
	/**
	 * @desc   : 条件查询 (采购入库用)
	 * @author : 王英杰
	 * @date   : 2024/3/8 10:58
	 */
	@Pagination
	public ResponseResultVO<PageList<InboundResponse>> selectByCond(InboundQuery inboundQuery) {
		return super.mergeListWithCount(inboundQuery, inboundMapper.selectByCond(inboundQuery), inboundMapper.countByCond(inboundQuery));
	}


	/**
	 * @desc : 查看来源单据,总单加明细  采购退货用
	 * @author : 于继渤
	 * @date : 2024/3/6 10:36
	 */
	@Pagination
	public ResponseResultVO<PageList<InboundResponse>> selectInboundAndItem(InboundQuery inboundQuery) {
		return super.mergeListWithCount(inboundQuery, inboundMapper.selectInboundAndItem(inboundQuery),
				inboundMapper.selectInboundAndItemCountByCond(inboundQuery));
	}
	/**
	 * @desc : 查看来源单据,总单加明细  采购入货用
	 * @author : 王英杰
	 * @date : 2024/3/6 10:36
	 */
	public ResponseResultVO selectById(String intoId) {
		//根据id查询
		InboundResponse inboundResponse = inboundMapper.selectById(intoId);
		//不存在
		if (inboundResponse == null) {
			return ResponseResultUtil.error(ErrorCodeEnum.INBOUND_CANNOT_BE_EMPTY.getCode(),
					ErrorCodeEnum.INBOUND_CANNOT_BE_EMPTY.getMessage());
		}
		//查询明细
		List<InboundItemResponse> inboundItemResponselist = inboundItemMapper.selectByCond(new InboundItemQuery().setIntoId(intoId));
		inboundResponse.setGoodsList(inboundItemResponselist);
		return ResponseResultUtil.success(inboundResponse);
	}
	/**
	 * @desc : 新建入库单
	 * @author : 王英杰
	 * @date : 2024/3/6 10:36
	 */
	@Transactional(
			rollbackFor = {Exception.class}
	)
	public ResponseResultVO<?> insert(InboundVO inboundVO) {
		//实体转换
		Inbound inbound = inboundConvert.convertToPo(inboundVO);

		//设置编码
		Map<String, Object> uniqueNoteCode = commonService.getUniqueNoteCode(Constant.docNameConstant.INBIUND.getName(), true);
		inbound.setIntoId(uniqueNoteCode.get("outId").toString());
		inbound.setIntoNo(uniqueNoteCode.get("outNote").toString());
		//新建
		inboundMapper.insert(inbound);


		return ResponseResultUtil.success();
	}



	/**
	 * @desc   : 获取单据信息（编辑用）
	 * @date   : 2024/3/14 16:38
	 * @author : 寇珊珊
	 */
	public ResponseResultVO<?> selectByUpdate(String id) {
		Map<String, Object> dataInfo = new HashMap<>();
		//总单
		InboundResponse inboundResponse = inboundMapper.selectById(id);
		dataInfo.put("data", inboundResponse);

		// 明细
		List<InboundItemResponse> inboundItemResponses = inboundItemMapper.selectByCond(new InboundItemQuery().setIntoId(id));
		dataInfo.put("dataItem", inboundItemResponses);
		return ResponseResultUtil.success(dataInfo);
	}




}
