package com.pj.tb_order_after_sale;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pj.api.client.admin.AdminInterface;
import com.pj.common.core.exception.ServiceException;
import com.pj.current.dto.APPLoginUserInfo;
import com.pj.current.satoken.StpAPPUserUtil;
import com.pj.enummj.DeleteStatus;
import com.pj.enummj.UserType;
import com.pj.tb_order.TbOrder;
import com.pj.tb_order.TbOrderMapper;
import com.pj.tb_order.TbOrderService;
import com.pj.tb_order_after_sale.param.AfterSaleParam;
import com.pj.tb_order_after_sale.param.TbOrderAfterSaleParam;
import com.pj.utils.so.SoMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import com.pj.utils.sg.*;

/**
 * Service: tb_order_after_sale -- 售后申请表，并关联一级市场订单
 * @author qzy 
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TbOrderAfterSaleService extends ServiceImpl<TbOrderAfterSaleMapper, TbOrderAfterSale> implements IService<TbOrderAfterSale>{

	/** 底层 Mapper 对象 */
	@Autowired
	TbOrderAfterSaleMapper tbOrderAfterSaleMapper;
	/** 订单表 */
	@Autowired
	private TbOrderService tbOrderService;
	/** 一级市场订单表 */
	@Autowired
	private TbOrderMapper tbOrderMapper;

	/** 增 */
	void add(TbOrderAfterSale t){
		save(t);
	}

	/** 删 */
	void delete(Long id){
		removeById(id);
	}

	/** 改 */
	void update(TbOrderAfterSale t){
		updateById(t);

	}

	/** 查 */
	TbOrderAfterSale getById(Long id){
		return super.getById(id);
	}

	/** 查集合 - 根据条件（参数为空时代表忽略指定条件） */  
	List<TbOrderAfterSale> getList(SoMap so) { 
		return tbOrderAfterSaleMapper.getList(so);	
	}

	/**
	 * app端:查集合 - 根据条件（参数为空时代表忽略指定条件
	 * @param soMap
	 * @param afterSaleStatus 可根据售后状态进行查询
	 * @param keyword  可根据关键字进行查询
	 * @return
	 */
	List<TbOrderAfterSale> getAppList(SoMap soMap,Integer afterSaleStatus,String keyword) {
		//获取当前登陆人
		APPLoginUserInfo appLoginInfo = StpAPPUserUtil.getAPPLoginInfo();
		if(appLoginInfo == null || appLoginInfo.getLoginId() == null)
			throw new ServiceException("当前登录用户信息已失效!");
		//设置当前人的查看信息
		soMap.set("apply_type",afterSaleStatus);
		soMap.set("keyword",keyword);
		soMap.set("sale_main_id",appLoginInfo.getLoginId());
		soMap.set("delete_status", DeleteStatus.DELETE_STATUS_ON.getCode());
		soMap.set("sortType",5);
		return tbOrderAfterSaleMapper.getList(soMap);
	}

	/** app查看个人订单 */
	List<TbOrder> selectSelfOrders(SoMap soMap){
		//下面调用已获取登陆人信息
		List<TbOrder> tbOrderList = tbOrderService.selectSelfOrders(soMap);
		return tbOrderList;
	}

	/** 边民发起售后申请 */
	boolean afterSale(AfterSaleParam afterSaleParam){
		TbOrderAfterSale tbOrderAfterSale = new TbOrderAfterSale();
		BeanUtils.copyProperties(afterSaleParam,tbOrderAfterSale);
		//设置售后状态为0,待处理
		tbOrderAfterSale.setAfterSaleStatus(0);
		tbOrderAfterSale.setTime(new Date());
		//执行保存
		int insert = tbOrderAfterSaleMapper.insert(tbOrderAfterSale);
		if(insert == 1)return true;
		return false;
	}

	/**
	 * 查看并处理边民退单、退款请求（确认退款）
	 */
	public boolean disposeOrders(TbOrderAfterSaleParam tbOrderAfterSaleParam){
		//获取当前登陆人
		APPLoginUserInfo appLoginInfo = StpAPPUserUtil.getAPPLoginInfo();
		if(appLoginInfo == null || appLoginInfo.getLoginId() == null)
			throw new RuntimeException("当前登录账号信息已失效!");
		//判断当前用户是否为商户
		if(!appLoginInfo.getUserType().toString().equals(UserType.USER_TYPE_ENTERPRISE.getCode().toString()))
			throw new ServiceException("您当前身份不是商户!");
		//保存售后结果
		TbOrderAfterSale tbOrderAfterSale = new TbOrderAfterSale();
		BeanUtils.copyProperties(tbOrderAfterSaleParam,tbOrderAfterSale);
		int updateById = tbOrderAfterSaleMapper.updateById(tbOrderAfterSale);
		if(updateById != 1)
			throw new RuntimeException("售后结果修改失败!");
		//获取商家处理结果
		Integer result = tbOrderAfterSaleParam.getResult();
		if(result == 1){
			//商家同意,开始执行退货退款
			//todo: 解冻互市组民的资金

		}else {
			//商家不同意,驳回,   todo: 给互市组组长发送推送提示

		}

		return updateById == 1;
	}

}
