package com.buy507.mall.portal.service.impl;

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

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.buy507.mall.mapper.OmsOrderReturnApplyMapper;
import com.buy507.mall.mapper.OmsOrderReturnReasonMapper;
import com.buy507.mall.model.OmsOrderReturnApply;
import com.buy507.mall.model.OmsOrderReturnApplyExample;
import com.buy507.mall.model.OmsOrderReturnReason;
import com.buy507.mall.model.OmsOrderReturnReasonExample;
import com.buy507.mall.model.UmsMember;
import com.buy507.mall.portal.domain.OmsOrderReturnApplyParam;
import com.buy507.mall.portal.domain.OrderReturnResult;
import com.buy507.mall.portal.service.OmsPortalOrderReturnService;
import com.buy507.mall.portal.service.UmsMemberService;
import com.github.pagehelper.PageHelper;

/**
 * 订单退货管理Service实现类
 */
@Service
public class OmsPortalOrderReturnServiceImpl implements OmsPortalOrderReturnService {
	
	@Autowired
    private UmsMemberService memberService;
	
    @Autowired
    private OmsOrderReturnApplyMapper returnApplyMapper;
    
    @Autowired
    private OmsOrderReturnReasonMapper returnReasonMapper;
    
    @Override
    public int create(OmsOrderReturnApplyParam returnApply) {
        OmsOrderReturnApply realApply = new OmsOrderReturnApply();
        BeanUtils.copyProperties(returnApply,realApply);
        realApply.setCreateTime(new Date());
        realApply.setStatus(0);
        return returnApplyMapper.insert(realApply);
    }
    
	@Override
	public List<String> getReturnReason() {
		OmsOrderReturnReasonExample example = new OmsOrderReturnReasonExample();
		//状态：0->不启用；1->启用
		example.createCriteria().andStatusEqualTo(1);
		example.setOrderByClause("sort desc");
		List<OmsOrderReturnReason> returnReasonList = returnReasonMapper.selectByExample(example);
		List<String> nameList = new ArrayList<>();
		for(OmsOrderReturnReason reason : returnReasonList) {
			nameList.add(reason.getName());
		}
		return nameList;
	}

	@Override
	public List<OrderReturnResult> getMemberOrderReturnList(Integer pageSize, Integer pageNum) {
		UmsMember loginMember = memberService.getCurrentMember();
		PageHelper.startPage(pageNum, pageSize);
		OmsOrderReturnApplyExample example = new OmsOrderReturnApplyExample();
		example.createCriteria().andMemberIdEqualTo(loginMember.getId());
		example.setOrderByClause("create_time desc");
		
		List<OmsOrderReturnApply> list = returnApplyMapper.selectByExample(example);
		List<OrderReturnResult> orderReturnList = new ArrayList<>();
		OrderReturnResult orderReturn = null;
		for(OmsOrderReturnApply apply : list) {
			orderReturn = new OrderReturnResult();
			BeanUtils.copyProperties(apply, orderReturn);
			//申请状态：0->待处理；1->退货中；2->已完成；3->已拒绝
			if(orderReturn.getStatus() == 0) {
				orderReturn.setStatusName("待处理");
			}
			if(orderReturn.getStatus() == 1) {
				orderReturn.setStatusName("退货中");
			}
			if(orderReturn.getStatus() == 2) {
				orderReturn.setStatusName("已完成");
			}
			if(orderReturn.getStatus() == 3) {
				orderReturn.setStatusName("已拒绝");
			}
			orderReturnList.add(orderReturn);
		}
		 
		return orderReturnList;
	}

	@Override
	public OrderReturnResult getMemberOrderReturnDetailInfo(Long orderReturnId) {
		UmsMember loginMember = memberService.getCurrentMember();
		OmsOrderReturnApplyExample example = new OmsOrderReturnApplyExample();
		example.createCriteria().andMemberIdEqualTo(loginMember.getId());
		example.createCriteria().andIdEqualTo(orderReturnId);
		List<OmsOrderReturnApply> list = returnApplyMapper.selectByExample(example);
		OrderReturnResult orderReturn = null;
		for(OmsOrderReturnApply apply : list) {
			orderReturn = new OrderReturnResult();
			BeanUtils.copyProperties(apply, orderReturn);
			//申请状态：0->待处理；1->退货中；2->已完成；3->已拒绝
			if(orderReturn.getStatus() == 0) {
				orderReturn.setStatusName("待处理");
			}
			if(orderReturn.getStatus() == 1) {
				orderReturn.setStatusName("退货中");
			}
			if(orderReturn.getStatus() == 2) {
				orderReturn.setStatusName("已完成");
			}
			if(orderReturn.getStatus() == 3) {
				orderReturn.setStatusName("已拒绝");
			}
		}
		return orderReturn;
	}
}
