package com.ruoyi.mall.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.sign.Md5Utils;
import com.ruoyi.mall.domain.MallOrderGoods;
import com.ruoyi.mall.domain.MallOrderOrder;
import com.ruoyi.mall.domain.vo.MallOrderGoodsVO;
import com.ruoyi.mall.service.IMallGoodsProductService;
import com.ruoyi.mall.service.IMallOrderGoodsService;
import com.ruoyi.mall.service.IMallOrderOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallOrderBackMapper;
import com.ruoyi.mall.domain.MallOrderBack;
import com.ruoyi.mall.service.IMallOrderBackService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 退款单Service业务层处理
 * 
 * @author chang
 * @date 2021-11-25
 */
@Service
public class MallOrderBackServiceImpl implements IMallOrderBackService 
{
    @Autowired
    private MallOrderBackMapper mallOrderBackMapper;

    @Autowired
    private IMallOrderOrderService orderOrderService;

    @Autowired
    private IMallOrderGoodsService orderGoodsService;

    @Autowired
    private IMallGoodsProductService goodsProductService;

    /**
     * 查询退款单
     * 
     * @param id 退款单主键
     * @return 退款单
     */
    @Override
    public MallOrderBack selectMallOrderBackById(Long id)
    {
        return mallOrderBackMapper.selectMallOrderBackById(id);
    }

    /**
     * 查询退款单列表
     * 
     * @param mallOrderBack 退款单
     * @return 退款单
     */
    @Override
    public List<MallOrderBack> selectMallOrderBackList(MallOrderBack mallOrderBack)
    {
        return mallOrderBackMapper.selectMallOrderBackList(mallOrderBack);
    }

    /**
     * 新增退款单
     * 
     * @param mallOrderBack 退款单
     * @return 结果
     */
    @Override
    public int insertMallOrderBack(MallOrderBack mallOrderBack)
    {
        return mallOrderBackMapper.insertMallOrderBack(mallOrderBack);
    }

    /**
     * 修改退款单
     * 
     * @param mallOrderBack 退款单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMallOrderBack(MallOrderBack mallOrderBack)
    {
        MallOrderOrder order = orderOrderService.selectMallOrderOrderById(mallOrderBack.getOrderId());

        order.setStatus(mallOrderBack.getOrginalStatus());
        if ("1".equals(mallOrderBack.getStatus())){
            order.setReturnAt(DateUtils.getTime());
            order.setStatus("6");

            //改库存
            List<MallOrderGoodsVO> list = orderGoodsService.selectMallOrderGoodsByOrderId(order.getId());
            for (MallOrderGoodsVO vo: list) {
                goodsProductService.updateMallGoodsProductByParams(vo.getProductId(),vo.getTotal());
            }
        }
        if ("3".equals(mallOrderBack.getStatus())){
            order.setStatus(mallOrderBack.getOrginalStatus());
        }

        orderOrderService.updateMallOrderOrder(order);
        mallOrderBack.setUpdateAt(DateUtils.getTime());
        return mallOrderBackMapper.updateMallOrderBack(mallOrderBack);
    }



    /**
     * 批量删除退款单
     * 
     * @param ids 需要删除的退款单主键
     * @return 结果
     */
    @Override
    public int deleteMallOrderBackByIds(Long[] ids)
    {
        return mallOrderBackMapper.deleteMallOrderBackByIds(ids);
    }

    /**
     * 删除退款单信息
     * 
     * @param id 退款单主键
     * @return 结果
     */
    @Override
    public int deleteMallOrderBackById(Long id)
    {
        return mallOrderBackMapper.deleteMallOrderBackById(id);
    }

    /*
    * 申请退款
    * */
    @Transactional
    @Override
    public int applyForRefund(Map<String, Object> params) {
        try {
            String userId = params.get("userId").toString();
            String orderId = params.get("orderId").toString();
            String reason = params.get("reason").toString();

            MallOrderOrder orgOrder = orderOrderService.selectMallOrderOrderById(Long.parseLong(orderId));
            MallOrderBack back = new MallOrderBack();
            back.setOrderId(Long.parseLong(orderId));
            back.setReason(reason);
            back.setStatus("0");
            back.setPrice(orgOrder.getPayment());
            back.setOrginalStatus(orgOrder.getStatus());
            back.setCreateAt(DateUtils.getTime());
            this.insertMallOrderBack(back);

            orgOrder.setStatus("5");
            orderOrderService.updateMallOrderOrder(orgOrder);
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }

    /*
     * 取消退款
     * */
    @Transactional
    @Override
    public int cancleRefund(Map<String, Object> params) {
        try {
            String refundId = params.get("refundId").toString();

            MallOrderBack orderBack = this.selectMallOrderBackById(Long.parseLong(refundId));
            orderBack.setStatus("2");
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            orderBack.setUpdateAt(formatter.format(new Date()));
            this.updateMallOrderBack(orderBack);

            MallOrderOrder orgOrder = orderOrderService.selectMallOrderOrderById(orderBack.getOrderId());
            orgOrder.setStatus(orderBack.getOrginalStatus());
            orderOrderService.updateMallOrderOrder(orgOrder);
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }

    /*
     * 同意、拒绝退款
     * */
    @Transactional
    @Override
    public int handleRefund(Map<String, Object> params) {
        try {
            String refundId = params.get("refundId").toString();

            MallOrderBack orderBack = this.selectMallOrderBackById(Long.parseLong(refundId));
            if ("0".equals(params.get("isAccept"))){
                //0是拒绝
                orderBack.setStatus("3");
            }else {
                orderBack.setStatus("1");
            }
            orderBack.setUpdateAt(new Date().toString());
            this.updateMallOrderBack(orderBack);

            MallOrderOrder orgOrder = orderOrderService.selectMallOrderOrderById(orderBack.getOrderId());
            if ("0".equals(params.get("isAccept"))){
                //0是拒绝
                orgOrder.setStatus(orderBack.getOrginalStatus());
            }else {
                orgOrder.setStatus("6");
            }
            orderOrderService.updateMallOrderOrder(orgOrder);
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }

    @Override
    public List<MallOrderBack> selectMallOrderBackByOrderId(Long orderId) {
        return mallOrderBackMapper.selectMallOrderBackByOrderId(orderId);
    }
}
