package com.heyu.api.controller.mm;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heyu.api.alibaba.request.mm.enums.ReverseStatusEnums;
import com.heyu.api.alibaba.request.mm.reverse.VvApproveRequest;
import com.heyu.api.alibaba.request.mm.reverse.VvReverseListRequest;
import com.heyu.api.alibaba.request.mm.reverse.VvReverseOrderDTO;
import com.heyu.api.data.dao.vv.*;
import com.heyu.api.data.entity.vv.VvReverseOrderEntity;
import com.heyu.api.data.entity.vv.VvReverseOrderLineEntity;
import com.heyu.api.data.entity.vv.VvReverseOrderProveEntity;
import com.heyu.api.data.entity.vv.VvTradeOrderLineEntity;
import com.heyu.api.data.utils.R;
import com.heyu.api.data.utils.SanUtils;
import com.heyu.api.utils.ISelect;
import com.heyu.api.utils.PPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@Slf4j
@RestController
@RequestMapping("/mm/reverse")
public class AdminReverseController {

    @Autowired
    private VvReverseOrderDao vvReverseOrderDao;

    @Autowired
    private VvReverseOrderLineDao vvReverseOrderLineDao;


    @Autowired
    private VvReverseOrderProveDao vvReverseOrderProveDao;


    @Autowired
    private VvTradeOrderDao vvTradeOrderDao;

    @Autowired
    private VvTradeOrderLineDao vvTradeOrderLineDao;


    /***
     * 逆向列表
     * @param vvReverseListRequest
     * @return
     */
    @RequestMapping("/list")
    public R list(@RequestBody VvReverseListRequest vvReverseListRequest) {
        PPageUtils pageUtils = PPageUtils.startPage(vvReverseListRequest.getPageNum(), vvReverseListRequest.getPageSize())
                .doSelect(new ISelect() {
                    @Override
                    public List doSelect(IPage page) {
                        return vvReverseOrderDao.selectByAdminCondition(page,
                                vvReverseListRequest.getTradeOrderId(),
                                vvReverseListRequest.getStatus(),
                                vvReverseListRequest.getTrackNumber(),
                                vvReverseListRequest.getRefundType(),
                                vvReverseListRequest.getMinCreateTimestamp(),
                                vvReverseListRequest.getMaxCreateTimestamp(),
                                vvReverseListRequest.getCreateTimestampSort()
                        );
                    }
                });


        List<VvReverseOrderEntity> vvReverseOrderEntities = pageUtils.getRows();
        List<Long> reverseOrderIds = SanUtils.list2listFilterNull(vvReverseOrderEntities, VvReverseOrderEntity::getId);

        List<VvReverseOrderLineEntity> reverseOrderLineEntities = vvReverseOrderLineDao.selectVvReverseOrderLineByReverseOrderIds(reverseOrderIds);
        Map<Long, List<VvReverseOrderLineEntity>> vvReverseOrderLineEntityMap = SanUtils.list2GroupBy(reverseOrderLineEntities, VvReverseOrderLineEntity::getReverseOrderId);


        List<VvReverseOrderProveEntity> vvReverseOrderProveEntities = vvReverseOrderProveDao.selectVvReverseOrderProveByReverseOrderId(reverseOrderIds);

        Map<Long, List<VvReverseOrderProveEntity>> vvProveMap = SanUtils.list2GroupBy(vvReverseOrderProveEntities, VvReverseOrderProveEntity::getReverseOrderId);

        for (VvReverseOrderEntity vvReverseOrderEntity : vvReverseOrderEntities) {
            VvReverseOrderDTO vvReverseOrderDTO = new VvReverseOrderDTO();
            BeanUtils.copyProperties(vvReverseOrderEntity, vvReverseOrderDTO);
            vvReverseOrderDTO.setVvReverseOrderLineEntities(vvReverseOrderLineEntityMap.get(vvReverseOrderEntity.getId()));
            vvReverseOrderDTO.setVvReverseOrderProveEntities(vvProveMap.get(vvReverseOrderEntity.getId()));
        }
        return R.ok().put("page", pageUtils);
    }


    /***
     * seller_agree_return_wait_buyer_post  卖家同意退货，买家邮寄
     *
     *
     * delivered 妥投成功
     *
     *
     * agree_refunded 同意 退款
     *
     * reject_delivered   拒绝妥投
     *
     *
     * close         关闭逆向
     */
    @RequestMapping("/approve")
    public R approve(@RequestBody VvApproveRequest vvApproveRequest) {
        VvReverseOrderEntity vvReverseOrderEntity = vvReverseOrderDao.selectVvReverseOrderById(vvApproveRequest.getReverseOrderId());

        if (ReverseStatusEnums.seller_agree_return_wait_buyer_post.getStatus().equals(vvApproveRequest.getStatus())) {
            if (ReverseStatusEnums.buyer_apply_reverse_wait_seller_agree.getStatus().equals(vvReverseOrderEntity.getStatus())) {
                return R.error("逆向订单状态不是 seller_agree_return_wait_buyer_post");
            }
            vvReverseOrderEntity.setGmtSellerAgreeReturn(new Date());
        } else if (ReverseStatusEnums.delivered.getStatus().equals(vvApproveRequest.getStatus())) {
            if (ReverseStatusEnums.shipping.getStatus().equals(vvReverseOrderEntity.getStatus())) {
                return R.error("逆向订单状态不是 shipping");
            }
            vvReverseOrderEntity.setGmtDelivered(new Date());
        } else if (ReverseStatusEnums.reject_delivered.getStatus().equals(vvApproveRequest.getStatus())) {
            if (ReverseStatusEnums.shipping.getStatus().equals(vvReverseOrderEntity.getStatus())) {
                return R.error("逆向订单状态不是 shipping");
            }
            vvReverseOrderEntity.setGmtRejectDelivered(new Date());
        } else if (ReverseStatusEnums.agree_refunded.getStatus().equals(vvApproveRequest.getStatus())) {
            if (ReverseStatusEnums.delivered.getStatus().equals(vvReverseOrderEntity.getStatus())) {
                return R.error("逆向订单状态不是 delivered");
            }
            vvReverseOrderEntity.setGmtAgreeRefunded(new Date());
        } else if (ReverseStatusEnums.close.getStatus().equals(vvApproveRequest.getStatus())) {
            vvReverseOrderEntity.setGmtClose(new Date());
        }
        vvReverseOrderEntity.setStatus(vvApproveRequest.getStatus());
        vvReverseOrderDao.updateVvReverseOrderById(vvReverseOrderEntity);


        List<VvReverseOrderLineEntity> vvReverseOrderLineEntities = vvReverseOrderLineDao.selectVvReverseOrderLineByReverseOrderId(vvApproveRequest.getReverseOrderId());
        List<Long> tradeOrderLineIds = SanUtils.list2list(vvReverseOrderLineEntities, VvReverseOrderLineEntity::getTradeOrderLineId);

        List<VvTradeOrderLineEntity> vvTradeOrderLineEntityList = vvTradeOrderLineDao.selectVvTradeOrderLineByIds(tradeOrderLineIds);

        for (VvTradeOrderLineEntity vvTradeOrderLineEntity : vvTradeOrderLineEntityList) {
            vvTradeOrderLineEntity.setReverseStatus(vvReverseOrderEntity.getStatus());
            vvTradeOrderLineDao.updateVvTradeOrderLineById(vvTradeOrderLineEntity);
        }
        return R.ok();
    }

}
