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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.miniapp.bean.material.MyDataCountBean;
import com.gree.mall.miniapp.bean.material.PartsRefundVO;
import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
import com.gree.mall.miniapp.commonmapper.MaterialMapper;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.plus.entity.WebsitPartsRet;
import com.gree.mall.miniapp.plus.entity.WebsitPartsRetItem;
import com.gree.mall.miniapp.plus.entity.WebsitPartsRetOldRecord;
import com.gree.mall.miniapp.plus.service.WebsitPartsRetItemService;
import com.gree.mall.miniapp.plus.service.WebsitPartsRetOldRecordService;
import com.gree.mall.miniapp.plus.service.WebsitPartsRetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class PartsRefundLogic {

    @Resource
    MaterialMapper materialMapper;
    @Resource
    CommonLogic commonLogic;
    @Resource
    WebsitPartsRetService websitPartsRetService;
    @Resource
    WebsitPartsRetItemService websitPartsRetItemService;
    @Resource
    WebsitPartsRetOldRecordService websitPartsRetOldRecordService;

    public IPage<PartsRefundVO> list(String type, String status, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        CurrentCompanyWechat wechat = commonLogic.getCurrentCompanyWechat(request);
        IPage<PartsRefundVO> page = materialMapper.partsRefundList(new Page<>(pageNum, pageSize), wechat.getCurrentCompanyWechatId(),
                wechat.getUserId(), type, status);
        if (CollectionUtil.isNotEmpty(page.getRecords())) {
            // 查出明细
            List<WebsitPartsRetItem> itemList = websitPartsRetItemService.lambdaQuery()
                    .in(WebsitPartsRetItem::getPartsRetId, page.getRecords().stream()
                            .map(PartsRefundVO::getPartsRetId)
                            .collect(Collectors.toList()))
                    .list();
            // 分组
            Map<String, List<WebsitPartsRetItem>> itemMap = itemList.stream().collect(Collectors.groupingBy(WebsitPartsRetItem::getPartsRetId));

            for (PartsRefundVO record : page.getRecords()) {
                record.setItems(itemMap.get(record.getPartsRetId()));
            }

            if ("OLD".equals(type)) {
                // 查出记录
                List<WebsitPartsRetOldRecord> recordList = websitPartsRetOldRecordService.lambdaQuery()
                        .in(WebsitPartsRetOldRecord::getPartsRetId, page.getRecords().stream()
                                .map(PartsRefundVO::getPartsRetId)
                                .collect(Collectors.toList()))
                        .list();
                // 分组
                Map<String, List<WebsitPartsRetOldRecord>> recordMap = recordList.stream().collect(Collectors.groupingBy(WebsitPartsRetOldRecord::getPartsRetId));

                for (PartsRefundVO record : page.getRecords()) {
                    record.setRecords(recordMap.get(record.getPartsRetId()));
                }

            }
        }
        return page;
    }

    public PartsRefundVO detail(String partsRetId, HttpServletRequest request) {
        CurrentCompanyWechat wechat = commonLogic.getCurrentCompanyWechat(request);
        WebsitPartsRet partsRet = websitPartsRetService.getById(partsRetId);
        PartsRefundVO vo = new PartsRefundVO();
        BeanUtils.copyProperties(partsRet, vo);
        List<WebsitPartsRetItem> itemList = websitPartsRetItemService.lambdaQuery()
                .eq(WebsitPartsRetItem::getPartsRetId, partsRetId)
                .list();
        vo.setItems(itemList);
        if ("OLD".equals(partsRet.getType())) {
            // 查出记录
            List<WebsitPartsRetOldRecord> recordList = websitPartsRetOldRecordService.lambdaQuery()
                    .in(WebsitPartsRetOldRecord::getPartsRetId, partsRetId)
                    .list();
            vo.setRecords(recordList);
        }
        return vo;
    }

    public MyDataCountBean count(String type, HttpServletRequest request) {
        CurrentCompanyWechat wechat = commonLogic.getCurrentCompanyWechat(request);
        MyDataCountBean bean = new MyDataCountBean();
        bean.setWaitRefundCount(
                websitPartsRetService.lambdaQuery()
                        .eq(WebsitPartsRet::getCompanyWechatId, wechat.getCurrentCompanyWechatId())
                        .eq(WebsitPartsRet::getWorkerId, wechat.getUserId())
                        .eq(WebsitPartsRet::getFlag, "SUBMIT")
                        .eq(WebsitPartsRet::getType, type)
                        .count()
        );
        bean.setEndRefundCount(
                websitPartsRetService.lambdaQuery()
                        .eq(WebsitPartsRet::getCompanyWechatId, wechat.getCurrentCompanyWechatId())
                        .eq(WebsitPartsRet::getWorkerId, wechat.getUserId())
                        .eq(WebsitPartsRet::getFlag, "OK")
                        .eq(WebsitPartsRet::getType, type)
                        .count()
        );
        return bean;
    }
}
