package com.heyu.api.listener.delay.handler;


import com.alibaba.fastjson.JSONObject;
import com.heyu.api.data.dao.vv.*;
import com.heyu.api.data.dto.PackageDelivedDelayDTO;
import com.heyu.api.data.entity.vv.VvPackageEntity;
import com.heyu.api.data.entity.vv.VvPromoterTradeOrderAwardEntity;
import com.heyu.api.data.entity.vv.VvPromoterTradeOrderLineAwardEntity;
import com.heyu.api.data.entity.vv.VvTradeOrderLineEntity;
import com.heyu.api.data.enums.ActionEnums;
import com.heyu.api.data.enums.AwardStatusEnums;
import com.heyu.api.data.enums.OrderStatusEnums;
import com.heyu.api.data.enums.RoleEnums;
import com.heyu.api.data.service.impl.AbstractOrderAction;
import com.heyu.api.data.service.vv.VvTradeOrderConvertService;
import com.heyu.api.data.utils.R;
import com.heyu.api.data.utils.SanUtils;
import com.heyu.api.listener.BaseDelayedHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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


@Component("deliveredHandler")
@Slf4j
public class DeliveredHandler extends BaseDelayedHandler {

    @Autowired
    private VvPackageDao vvPackageDao;

    @Autowired
    private VvTradeOrderLineDao vvTradeOrderLineDao;


    @Autowired
    private VvTradeOrderConvertService vvTradeOrderConvertService;

    @Autowired
    private VvPromoterTradeOrderAwardDao vvPromoterTradeOrderAwardDao;

    @Autowired
    private VvPromoterTradeOrderLineAwardDao vvPromoterTradeOrderLineAwardDao;


    @Autowired
    private VvBuyerDao vvBuyerDao;

    @Override
    public R handler(String message) {
        log.info("DeliveredHandler receive message:{}", message);
        PackageDelivedDelayDTO packageDelivedDelayDTO = JSONObject.parseObject(message, PackageDelivedDelayDTO.class);
        // 更新包信息
        VvPackageEntity vvPackage = vvPackageDao.selectVvPackageById(packageDelivedDelayDTO.getPackageId());
        vvPackage.setIscheck("3");

        vvPackage.setGmtDelivered(new Date());
        vvPackage.setDeliveredBy(RoleEnums.metaq.getRole());
        vvPackageDao.updateVvPackageById(vvPackage);
        // 更新子单信息
        List<VvTradeOrderLineEntity> vvTradeOrderLineEntityList = vvTradeOrderLineDao.selectVvTradeOrderLineByTrackNumber(vvPackage.getTrackNumber());
        for (VvTradeOrderLineEntity vvTradeOrderLineEntity : vvTradeOrderLineEntityList) {
            if (AbstractOrderAction.checkFailed(vvTradeOrderLineEntity, false, ActionEnums.Admin_Delivered)) {
                return R.error("订单状态不是 Admin_Delivered 不对,status : {}",vvTradeOrderLineEntity.getStatus());
            }
            vvTradeOrderLineEntity.setDeliveredUser(RoleEnums.metaq.getRole());
            vvTradeOrderLineEntity.setGmtDelivered(new Date());
            vvTradeOrderLineEntity.setStatus(OrderStatusEnums.delivered.getStatus());
            vvTradeOrderLineDao.updateVvTradeOrderLineById(vvTradeOrderLineEntity);
            vvTradeOrderConvertService.addOrderLineStatusLog(vvTradeOrderLineEntity.getId(),
                    OrderStatusEnums.delivered,
                    -1L,
                    RoleEnums.system.getRole()
            );
        }

        Long promoterId = SanUtils.findAnyNotNull(vvTradeOrderLineEntityList,VvTradeOrderLineEntity::getPromoterId );
        if(promoterId !=null){
            List<VvPromoterTradeOrderAwardEntity> vvPromoterTradeOrderAwardEntities = vvPromoterTradeOrderAwardDao.selectVvPromoterTradeOrderAwardByPromoterId(promoterId);
            for (VvPromoterTradeOrderAwardEntity vvPromoterTradeOrderAwardEntity : vvPromoterTradeOrderAwardEntities) {
                vvPromoterTradeOrderAwardEntity.setStatus(AwardStatusEnums.arrive.getStatus());
                vvPromoterTradeOrderAwardDao.updateVvPromoterTradeOrderAwardById(vvPromoterTradeOrderAwardEntity);

                vvBuyerDao.updateVvBuyerByIdPromoterArriveAccountAmountPromoterNotArriveAmount(
                        vvPromoterTradeOrderAwardEntity.getAwardAmount()
                        ,vvPromoterTradeOrderAwardEntity.getAwardAmount(),
                        vvPromoterTradeOrderAwardEntity.getId());
            }

            List<VvPromoterTradeOrderLineAwardEntity> vvPromoterTradeOrderLineAwardEntities = vvPromoterTradeOrderLineAwardDao.selectVvPromoterTradeOrderLineAwardByPromoterId(promoterId);
            for (VvPromoterTradeOrderLineAwardEntity vvPromoterTradeOrderLineAwardEntity : vvPromoterTradeOrderLineAwardEntities) {
                vvPromoterTradeOrderLineAwardEntity.setStatus(AwardStatusEnums.arrive.getStatus());
                vvPromoterTradeOrderLineAwardDao.updateVvPromoterTradeOrderLineAwardById(vvPromoterTradeOrderLineAwardEntity);
            }
        }



        return R.ok();
    }
}
