package com.pj.tb_order;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pj.api.dto.MessageDto;
import com.pj.common.core.exception.ServiceException;
import com.pj.enummj.DeleteStatus;
import com.pj.enummj.IsLock;
import com.pj.tb_enterprise.TbEnterprise;
import com.pj.tb_enterprise.TbEnterpriseMapper;
import com.pj.tb_goods_transit.TbGoodsTransit;
import com.pj.tb_goods_transit.TbGoodsTransitMapper;
import com.pj.tb_group.TbGroupMapper;
import com.pj.tb_order.vo.OrderVo;
import com.pj.tb_order_people_confirm.TbOrderPeopleConfirmMapper;
import com.pj.tb_people.TbPeople;
import com.pj.tb_people.TbPeopleMapper;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.pj.api.consts.FeignFactory.adminInterface;

/**
 * @Author Mechrevo
 * @Date 2023 08 02 14 20
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class MethodOrderService {

    @Autowired
    private TbOrderMapper tbOrderMapper;
    @Autowired
    private TbPeopleMapper tbPeopleMapper;
    @Autowired
    private TbEnterpriseMapper tbEnterpriseMapper;
    @Autowired
    private TbGoodsTransitMapper tbGoodsTransitMapper;
    @Autowired
    private TbOrderPeopleConfirmMapper tbOrderPeopleConfirmMapper;
    @Autowired
    private TbGroupMapper tbGroupMapper;

    /**
     * 根据类型返回对应的订单列表
     * @param userType
     */
    public List<TbOrder> checkType(Integer userType, Long fk, SoMap so){
        if(userType == null)return Collections.emptyList();
        if(fk == null)return Collections.emptyList();
        switch (userType){
            case 1: //边民
            case 2: //互市组组长
                TbPeople tbPeople = tbPeopleMapper.selectById(fk);
                if(tbPeople == null)throw new ServiceException("当前用户未认证!");
                so.put("buyUserId",fk);
                break;
            case 5: // 一级市场商家
                so.put("enterpriseId",fk);
                break;
            case 6: // 合作社(暂未加条件过滤，可看所有边民订单)
                break;
            default:
                throw new ServiceException("服务器繁忙~");
        }
        so.put("deleteStatus",DeleteStatus.DELETE_STATUS_ON.getCode());
        List<TbOrder> tbOrderList = tbOrderMapper.getList(so);
        return tbOrderList;
    }

    /**
     * 给互助组下属所有人发送message
     * @param groupId  边民组ID
     * @param peopleName 边民表的边民名称
     * @param flag true=下单情况下发送message , false=取消订单时发送message
     */
    public void sendGroupMessage(Long groupId,String peopleName,boolean flag){
        //遍历取出边民组的的所有ID
        List<Long> peopleIdList = tbPeopleMapper.selectList
                        (new LambdaQueryWrapper<TbPeople>()
                                .eq(TbPeople::getGroupId, groupId)
                                .eq(TbPeople::getIsLock, IsLock.IS_LOCK_ON.getCode())
                                .eq(TbPeople::getDeleteStatus, DeleteStatus.DELETE_STATUS_ON.getCode()))
                .stream().map(TbPeople::getId).collect(Collectors.toList());
        //遍历发送
        for (Long item : peopleIdList){
            MessageDto dto = new MessageDto();
            dto.setUrl("/pages/market/one/merchant/order/list");
            dto.setUserId(item);
            if(flag){
                dto.setContents("你的组长 <<" + peopleName + ">> 已在app内完成下单,请前往查看。");
            }else {
                dto.setContents("你的购买的商品已被 <<" + peopleName + ">> 取消,请往app内进行查看。");
            }
            adminInterface.messageSave(dto);
        }
    }

}
