package com.ym.mec.biz.service.impl;

import com.ym.mec.biz.dal.dao.GoodsCategoryDao;
import com.ym.mec.biz.dal.dao.SubjectGoodsMapperDao;
import com.ym.mec.biz.dal.dto.SubjectGoodsDto;
import com.ym.mec.biz.dal.entity.Goods;
import com.ym.mec.biz.dal.entity.GoodsCategory;
import com.ym.mec.biz.dal.entity.SubjectGoodsMapper;
import com.ym.mec.biz.dal.enums.AccountType;
import com.ym.mec.biz.dal.enums.GoodsType;
import com.ym.mec.biz.dal.page.GoodsCategoryQueryInfo;
import com.ym.mec.biz.service.GoodsCategoryService;
import com.ym.mec.biz.service.GoodsService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.util.collection.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class GoodsCategoryServiceImpl extends BaseServiceImpl<Integer, GoodsCategory>  implements GoodsCategoryService {
	
	@Autowired
	private GoodsCategoryDao goodsCategoryDao;
	@Autowired
	private SubjectGoodsMapperDao subjectGoodsMapperDao;

	@Override
	public BaseDAO<Integer, GoodsCategory> getDAO() {
		return goodsCategoryDao;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void upsetGoodsCategory(GoodsCategory goodsCategory) {
		goodsCategory.setParentId(0);
		if(goodsCategory.getId() != null){
			goodsCategory.setUpdateTime(new Date());
			goodsCategoryDao.update(goodsCategory);
			//删除关联
			subjectGoodsMapperDao.delByGoodsCategoryId(goodsCategory.getId());
		}else {
			goodsCategoryDao.insert(goodsCategory);
		}
		List<Integer> subjectIds = goodsCategory.getSubjectIds();
		subjectGoodsMapperDao.batchAdd(goodsCategory.getId(),subjectIds);
	}

	@Override
	public PageInfo<SubjectGoodsDto> querySubjectGoods(GoodsCategoryQueryInfo queryInfo) {
		PageInfo<SubjectGoodsDto> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
		Map<String, Object> params = new HashMap<>();
		MapUtil.populateMap(params, queryInfo);

		List<SubjectGoodsDto> dataList = null;
		int count = subjectGoodsMapperDao.countSubjectGoods(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = subjectGoodsMapperDao.querySubjectGoods(params);
			dataList.forEach(e->{
				e.setInstrument(e.getGoodsList().stream().filter(goods -> goods.getType() == GoodsType.INSTRUMENT).collect(Collectors.toList()));
				e.setAccessories(e.getGoodsList().stream().filter(goods -> goods.getType() == GoodsType.ACCESSORIES).collect(Collectors.toList()));
				e.setGoodsList(null);
			});
		}
		if (count == 0) {
			dataList = new ArrayList<>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

	@Override
	public void updateSubjectGoods(Long subjectGoodsMapperId, String goodsIds) {
		SubjectGoodsMapper subjectGoodsMapper = new SubjectGoodsMapper();
		subjectGoodsMapper.setId(subjectGoodsMapperId);
		subjectGoodsMapper.setGoodsIdList(goodsIds);
		subjectGoodsMapper.setUpdateTime(new Date());
		subjectGoodsMapperDao.update(subjectGoodsMapper);
	}
}
