package com.gree.mall.miniapp.logic.material.parts;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.miniapp.bean.material.parts.PartsSalesOrderBean;
import com.gree.mall.miniapp.bean.material.parts.PartsSalesOrderItemBean;
import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
import com.gree.mall.miniapp.commonmapper.MaterialMapper;
import com.gree.mall.miniapp.enums.IsYesNoEnum;
import com.gree.mall.miniapp.enums.base.BaseEnum;
import com.gree.mall.miniapp.enums.material.PartsOrderStatusEnum;
import com.gree.mall.miniapp.enums.material.PartsPushFlagEnum;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.plus.entity.AdminWebsit;
import com.gree.mall.miniapp.plus.entity.WebsitPartsSales;
import com.gree.mall.miniapp.plus.entity.WebsitPartsSalesItem;
import com.gree.mall.miniapp.plus.entity.WebsitSalesPayOrder;
import com.gree.mall.miniapp.plus.service.AdminWebsitService;
import com.gree.mall.miniapp.plus.service.WebsitPartsSalesItemService;
import com.gree.mall.miniapp.plus.service.WebsitPartsSalesService;
import com.gree.mall.miniapp.plus.service.WebsitSalesPayOrderService;
import io.jsonwebtoken.lang.Collections;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;


@Service
@Slf4j
public class WebsitPartsSalesLogic {

    @Resource
    CommonLogic commonLogic;
    @Resource
    MaterialMapper materialMapper;
    @Resource
    WebsitPartsSalesService websitPartsSalesService;
    @Resource
    WebsitPartsSalesItemService websitPartsSalesItemService;
    @Resource
    AdminWebsitService adminWebsitService;
    @Resource
    WebsitSalesPayOrderService websitSalesPayOrderService;

    public List<AdminWebsit> existWebsitList(String identity, String websitId) {
        CurrentCompanyWechat wechat = commonLogic.getCurrentCompanyWechat();
        List<WebsitPartsSales> partsSalesOrderList = websitPartsSalesService.lambdaQuery()
                .eq(WebsitPartsSales::getCompanyWechatId, wechat.getCompanyWechatId())
                .eq(WebsitPartsSales::getIdentity, identity)
                .eq(StringUtils.isNotBlank(websitId), WebsitPartsSales::getWebsitId, websitId)
                .eq(WebsitPartsSales::getPushFlag, PartsPushFlagEnum.SALES.getKey())
                .groupBy(WebsitPartsSales::getWebsitId)
                .list();
        List<AdminWebsit> shopList = new ArrayList<>();
        for (WebsitPartsSales partsSalesOrder : partsSalesOrderList) {
            AdminWebsit shop = adminWebsitService.lambdaQuery()
                    .eq(AdminWebsit::getCompanyWechatId, wechat.getCompanyWechatId())
                    .eq(AdminWebsit::getWebsitId, partsSalesOrder.getWebsitId())
                    .one();
            shopList.add(shop);
        }
        return shopList;
    }

    public IPage<PartsSalesOrderBean> appList(String identity, String salesId, PartsOrderStatusEnum status, PartsPushFlagEnum pushFlag,
                                              String partsNumber, Integer pageNo, Integer pageSize) {
        CurrentCompanyWechat wechat = commonLogic.getCurrentCompanyWechat();
        IPage<PartsSalesOrderBean> page = materialMapper.appSalesList(new Page(pageNo, pageSize), wechat.getCompanyWechatId(), identity, salesId, Objects.isNull(status) ? null : status.getKey(), Objects.isNull(pushFlag) ? null : pushFlag.getKey(), partsNumber);

        List<PartsSalesOrderBean> partsSalesOrderBeanList = new ArrayList<>();
        for (Object record : page.getRecords()) {
            PartsSalesOrderBean salesOrderBean = new PartsSalesOrderBean();
            BeanUtils.copyProperties(record, salesOrderBean, PartsSalesOrderBean.class);
            List<WebsitPartsSalesItem> partsSalesOrderItems = websitPartsSalesItemService.lambdaQuery()
                    .eq(WebsitPartsSalesItem::getSalesId, salesOrderBean.getId())
                    .gt(Objects.nonNull(pushFlag) && pushFlag.getKey().equals(PartsPushFlagEnum.SALES.getKey()), WebsitPartsSalesItem::getQty, 0)
                    .list();
            List<PartsSalesOrderItemBean> itemList = new ArrayList<>();
            if(!Collections.isEmpty(partsSalesOrderItems)) {
                for(WebsitPartsSalesItem partsSalesOrderItem: partsSalesOrderItems) {
                    PartsSalesOrderItemBean item = new PartsSalesOrderItemBean();
                    BeanUtils.copyProperties(partsSalesOrderItem, item, PartsSalesOrderItemBean.class);
                    itemList.add(item);
                }
            }
            salesOrderBean.setItemList(itemList);
            partsSalesOrderBeanList.add(salesOrderBean);
        }
        page.setRecords(partsSalesOrderBeanList);
        return page;
    }

    public PartsSalesOrderBean appDetail(String identity, String salesId, String pushFlag) {
        WebsitPartsSales partsSalesOrder = websitPartsSalesService.lambdaQuery()
                .eq(WebsitPartsSales::getIdentity, identity)
                .eq(WebsitPartsSales::getId, salesId)
                .eq(StringUtils.isNotBlank(pushFlag), WebsitPartsSales::getPushFlag, pushFlag)
                .one();
        PartsSalesOrderBean salesOrderBean = new PartsSalesOrderBean();
        BeanUtils.copyProperties(partsSalesOrder, salesOrderBean, PartsSalesOrderBean.class);
        salesOrderBean.setBuyTypeName(partsSalesOrder.getOrderSource().equals("ONLINE") ? "在线订单" : "自建订单");
//        salesOrderBean.setStateName(this.getStateName(partsSalesOrder.getState()));
        salesOrderBean.setStateName(this.getStateName(partsSalesOrder.getStatus(), partsSalesOrder.getWorkerConfirmDate(), partsSalesOrder.getWebsitConfirmDate()));
//        salesOrderBean.setPushFlagName(PartsPushFlagEnum.valueOf(pushFlag).getDesc());
        salesOrderBean.setPushFlagName(StringUtils.isBlank(pushFlag) ? "" : BaseEnum.keyToEnumNotNull(PartsPushFlagEnum.class, pushFlag).getRemark());
        //提货状态
        if(!Objects.isNull(salesOrderBean.getWorkerConfirmDate()) && !Objects.isNull(salesOrderBean.getWebsitConfirmDate())) {
            salesOrderBean.setPickupStateName("已提货");
        }else if(!Objects.isNull(salesOrderBean.getWorkerConfirmDate())) {
            salesOrderBean.setPickupStateName("师傅已提货");
        }else if(!Objects.isNull(salesOrderBean.getWebsitConfirmDate())) {
            salesOrderBean.setPickupStateName("网点已提货");
        }else {
            salesOrderBean.setPickupStateName("未提货");
        }
        List<WebsitPartsSalesItem> partsSalesOrderItems = websitPartsSalesItemService.lambdaQuery()
                .eq(WebsitPartsSalesItem::getSalesId, salesOrderBean.getId())
                .gt(StringUtils.isNotBlank(pushFlag)
                        && pushFlag.equals(PartsPushFlagEnum.SALES.toString()), WebsitPartsSalesItem::getQty, 0)
                .list();

        List<PartsSalesOrderItemBean> itemList = new ArrayList<>();
        if(!Collections.isEmpty(partsSalesOrderItems)) {
            for(WebsitPartsSalesItem partsSalesOrderItem: partsSalesOrderItems) {
                PartsSalesOrderItemBean item = new PartsSalesOrderItemBean();
                BeanUtils.copyProperties(partsSalesOrderItem, item, PartsSalesOrderItemBean.class);
                itemList.add(item);
            }
            salesOrderBean.setItemList(itemList);
        }
        List<WebsitSalesPayOrder> payList = websitSalesPayOrderService.lambdaQuery()
                .eq(WebsitSalesPayOrder::getPayFlag, IsYesNoEnum.YES.getKey())
                .eq(WebsitSalesPayOrder::getOrderId, salesOrderBean.getId())
                .list();
        salesOrderBean.setPayList(payList);
        return salesOrderBean;
    }

    private String getStateName(String status, Date workerConfirmDate, Date shopConfirmDate) {
        String stateName = "";
        switch (status) {
            case "SAVE":
                stateName = "已保存";
                break;
            case "SUBMIT":
                stateName = "已提交";
                break;
            case "PAYED":
                if(!Objects.isNull(workerConfirmDate) && !Objects.isNull(shopConfirmDate)) {
                    stateName = "已完成";
                }else if(!Objects.isNull(workerConfirmDate)) {
                    stateName = "师傅已提货";
                }else if(!Objects.isNull(shopConfirmDate)) {
                    stateName = "网点已提货";
                }else {
                    stateName = "已支付";
                }
                break;
            case "END":
                stateName = "已完成";
                break;
            case "CANCEL":
                stateName = "已取消";
                break;
            default:
        }
        return stateName;
    }
}
