package cn.superdesk.uniorder.dubbo.provider.unified;

import cn.hutool.core.date.DateUtil;
import cn.superdesk.uniorder.common.enums.UnifiedCode;
import cn.superdesk.uniorder.dao.entity.unified.UnifiedOrderAppraise;
import cn.superdesk.uniorder.dao.entity.unified.UnifiedOrderGoodsAppraise;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedOrderAppraiseAdd;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedOrderAppraiseSMAdd;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedOrderGoodsAppraiseAdd;
import cn.superdesk.uniorder.dubbo.facade.unified.DubboUnifiedOrderAppraiseService;
import cn.superdesk.uniorder.service.order.base.UnifiedOrderAppraiseService;
import com.alibaba.dubbo.config.annotation.Service;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service(version = "1.0.0", retries = -1, timeout = 10000)
public class DubboUnifiedOrderAppraiseServiceImpl implements DubboUnifiedOrderAppraiseService {

	@Autowired
	private UnifiedOrderAppraiseService appraiseService;

	@Override
	public Map<String, Object> addObj(UnifiedOrderAppraiseAdd appraiseAdd) {

		Map<String, Object>  retmap   = new HashMap<>();
		UnifiedOrderAppraise appraise = new UnifiedOrderAppraise();
		if (StringUtils.isNotEmpty(appraiseAdd.getAppend_state())) {
			appraise.setAppendState(Byte.valueOf(appraiseAdd.getAppend_state()));
		} else {
			appraise.setAppendState((byte) 2);
		}
		appraise.setAppraiseContent(appraiseAdd.getAppraise_content());
		appraise.setAppraiseLevel(Byte.valueOf(appraiseAdd.getAppraise_level()));
		appraise.setCreateTime(DateUtil.date());
		if (StringUtils.isNotEmpty(appraiseAdd.getUser_id())) {
			appraise.setCreateUserId(Long.parseLong(appraiseAdd.getUser_id()));
		} else {
			retmap.put("code", UnifiedCode.REQUIRED_NOT.getCode());
			retmap.put("msg", UnifiedCode.REQUIRED_NOT.getMsg());
			return retmap;
		}
		appraise.setPics(appraiseAdd.getPics());
		//非必填
		if (StringUtils.isNotEmpty(appraiseAdd.getService_content())) {
			appraise.setServiceContent(appraiseAdd.getService_content());
		}
		if (StringUtils.isNotEmpty(appraiseAdd.getService_level())) {
			appraise.setServiceLevel(Byte.valueOf(appraiseAdd.getService_level()));
		}

		appraise.setSn(appraiseAdd.getSn());
		appraise.setSysState((byte) 2);
		if (StringUtils.isNotEmpty(appraiseAdd.getTran_content())) {
			appraise.setTranContent(appraiseAdd.getTran_content());
		}
		if (StringUtils.isNotEmpty(appraiseAdd.getTran_level())) {
			appraise.setTranLevel(Byte.valueOf(appraiseAdd.getTran_level()));
		}
		appraise.setUserName(appraiseAdd.getUser_name());

		appraiseService.insertSelective(appraise);
		retmap.put("code", 1);
		return retmap;
	}

	@Override
	public Map<String, Object> addObjSM(UnifiedOrderAppraiseSMAdd appraiseAdd,
										List<UnifiedOrderGoodsAppraiseAdd> goodsLs) {

		Map<String, Object>  retmap   = new HashMap<>();
		UnifiedOrderAppraise appraise = new UnifiedOrderAppraise();
		if (StringUtils.isNotEmpty(appraiseAdd.getAppend_state())) {
			appraise.setAppendState(Byte.valueOf(appraiseAdd.getAppend_state()));
		} else {
			appraise.setAppendState((byte) 2);
		}
		appraise.setAppraiseContent(appraiseAdd.getAppraise_content());
		if (StringUtils.isNotEmpty(appraiseAdd.getAppraise_level())) {
			appraise.setAppraiseLevel(Byte.valueOf(appraiseAdd.getAppraise_level()));
		}

		appraise.setCreateTime(DateUtil.date());
		if (StringUtils.isNotEmpty(appraiseAdd.getUser_id())) {
			appraise.setCreateUserId(Long.parseLong(appraiseAdd.getUser_id()));
		} else {
			retmap.put("code", UnifiedCode.REQUIRED_NOT.getCode());
			retmap.put("msg", UnifiedCode.REQUIRED_NOT.getMsg());
			return retmap;
		}
		if (StringUtils.isNotEmpty(appraiseAdd.getDescribes_level())) {
			appraise.setDescribesLevel(Byte.valueOf(appraiseAdd.getDescribes_level()));
		}
		appraise.setPics(appraiseAdd.getPics());
		//非必填
		if (StringUtils.isNotEmpty(appraiseAdd.getService_content())) {
			appraise.setServiceContent(appraiseAdd.getService_content());
		}
		if (StringUtils.isNotEmpty(appraiseAdd.getService_level())) {
			appraise.setServiceLevel(Byte.valueOf(appraiseAdd.getService_level()));
		}

		appraise.setSn(appraiseAdd.getSn());
		appraise.setSysState((byte) 2);
		if (StringUtils.isNotEmpty(appraiseAdd.getTran_content())) {
			appraise.setTranContent(appraiseAdd.getTran_content());
		}
		if (StringUtils.isNotEmpty(appraiseAdd.getTran_level())) {
			appraise.setTranLevel(Byte.valueOf(appraiseAdd.getTran_level()));
		}
		appraise.setUserName(appraiseAdd.getUser_name());

		List<UnifiedOrderGoodsAppraise> goodsApp = new ArrayList<>();
		for (UnifiedOrderGoodsAppraiseAdd item : goodsLs) {
			UnifiedOrderGoodsAppraise ga = new UnifiedOrderGoodsAppraise();
			ga.setAppendState(appraise.getAppendState());
			if (StringUtils.isNotEmpty(item.getAppraise_level())) {
				ga.setAppraiseLevel(Byte.valueOf(item.getAppraise_level()));
			}
			ga.setCreateTime(DateUtil.date());
			ga.setCreateUserId(appraise.getCreateUserId());
			ga.setSn(appraise.getSn());
			ga.setSysState((byte) 2);

			if (StringUtils.isNotEmpty(item.getGoods_id()))
				ga.setGoodsId(Long.parseLong(item.getGoods_id()));

			goodsApp.add(ga);
		}
		appraiseService.addObjSM(appraise, goodsApp);
		retmap.put("code", 1);
		return retmap;
	}

}
