package com.gree.mall.manager.logic.websit;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.websit.*;
import com.gree.mall.manager.commonmapper.WebsitSalesRetCMapper;
import com.gree.mall.manager.enums.SettlementStatusEnum;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.SettlementOrder;
import com.gree.mall.manager.plus.entity.SettlementOrderItem;
import com.gree.mall.manager.plus.entity.WithdrawalOrder;
import com.gree.mall.manager.plus.entity.WithdrawalOrderItem;
import com.gree.mall.manager.plus.service.SettlementOrderItemService;
import com.gree.mall.manager.plus.service.SettlementOrderService;
import com.gree.mall.manager.plus.service.WithdrawalOrderItemService;
import com.gree.mall.manager.plus.service.WithdrawalOrderService;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class WithdrawalOrderLogic {

    @Autowired
    CommonLogic commonLogic;

    @Autowired
    WebsitSalesRetCMapper websitSalesRetCMapper;


    @Autowired
    WithdrawalOrderService withdrawalOrderService;

    @Autowired
    WithdrawalOrderItemService withdrawalOrderItemService;

    @Autowired
    SettlementOrderService settlementOrderService;



    public IPage<WithdrawalOrderVO> pageV2(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<String> companyWechatIds = adminUser.getCompanyWechatIds();

        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean, WithdrawalOrderVO.class);
        IPage<WithdrawalOrderVO> withdrawalV2 = websitSalesRetCMapper.pageWithdrawalV2(new Page(zfireParamBean.getPageNum(),
                        zfireParamBean.getPageSize()),
                zfireParam
                , companyWechatIds);

        return withdrawalV2;
    }

    public WithdrawalOrderDetail detail(String id) {

        WithdrawalOrder withdrawalOrder = withdrawalOrderService.getById(id);

        WithdrawalOrderDetail withdrawalOrderDetail = BeanUtil.toBean(withdrawalOrder, WithdrawalOrderDetail.class);

        withdrawalOrderDetail.setWithdrawalOrderItems(withdrawalOrderItemService.lambdaQuery().eq(WithdrawalOrderItem::getWithdrawalOrderId,id).list());
        return withdrawalOrderDetail;
    }


    @Transactional(rollbackFor = Exception.class)
    public void update(String id,String certImg,String remark) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        WithdrawalOrder withdrawalOrder = new WithdrawalOrder();
        withdrawalOrder.setId(id);
        withdrawalOrder.setStatus(1);
        withdrawalOrder.setGrantBy(adminUser.getNickName());
        withdrawalOrder.setGrantTime(new Date());
        withdrawalOrder.setCertImg(certImg);
        withdrawalOrder.setRemark(remark);
        withdrawalOrder.updateById();
        List<WithdrawalOrderItem> list = withdrawalOrderItemService.lambdaQuery().eq(WithdrawalOrderItem::getWithdrawalOrderId, id).list();

        if (CollectionUtils.isEmpty(list))
            return;
        for (WithdrawalOrderItem withdrawalOrderItem : list) {
            settlementOrderService.lambdaUpdate().eq(SettlementOrder::getSettlementOrderId,withdrawalOrderItem.getOrderId())
                    .set(SettlementOrder::getStatus,SettlementStatusEnum.OVER.getKey())
                    .set(SettlementOrder::getSettlementTime,new Date())
                    .update();

        }
    }
}
