package com.yiqi.app.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.app.domain.AppDeliveryRelation;
import com.yiqi.app.domain.dto.AppDeliveryManDTO;
import com.yiqi.app.domain.dto.AppDeliveryManListQueryDTO;
import com.yiqi.app.domain.vo.AppDeliveryManListVO;
import com.yiqi.app.service.IAppDeliveryRelationService;
import com.yiqi.common.enums.DeleteStatus;
import com.yiqi.common.enums.StatusType;
import com.yiqi.common.exception.GlobalException;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.common.utils.bean.BeanUtils;
import com.yiqi.system.domain.SysStore;
import com.yiqi.system.service.ISysStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiqi.app.mapper.AppDeliveryManMapper;
import com.yiqi.app.domain.AppDeliveryMan;
import com.yiqi.app.service.IAppDeliveryManService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 配送员Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-07
 */
@Service
public class AppDeliveryManServiceImpl extends ServiceImpl<AppDeliveryManMapper, AppDeliveryMan> implements IAppDeliveryManService {
    @Autowired
    private IAppDeliveryRelationService appDeliveryRelationService;
    @Autowired
    private ISysStoreService sysStoreService;
    /**
     * 查询配送员
     *
     * @param id 配送员主键
     * @return 配送员
     */
    @Override
    public AppDeliveryManDTO selectAppDeliveryManById(Long id) {
        AppDeliveryManDTO appDeliveryManDTO = new AppDeliveryManDTO();
        AppDeliveryMan appDeliveryMan = baseMapper.selectAppDeliveryManById(id);
        if (appDeliveryMan == null) {
            throw new GlobalException("无法查询该配送员信息");
        }
        BeanUtils.copyProperties(appDeliveryMan, appDeliveryManDTO);

        List<AppDeliveryRelation> list = appDeliveryRelationService.list(new QueryWrapper<AppDeliveryRelation>().lambda()
                .eq(AppDeliveryRelation::getAppUserId, appDeliveryMan.getAppUserId()));
        appDeliveryManDTO.setRelationList(list);
        return appDeliveryManDTO;
    }

    /**
     * 查询配送员列表
     *
     * @param appDeliveryManListQueryDTO 配送员
     * @return 配送员
     */
    @Override
    public List<AppDeliveryManListVO> selectAppDeliveryManList(AppDeliveryManListQueryDTO appDeliveryManListQueryDTO) {
        appDeliveryManListQueryDTO.setExpireDate(DateUtils.getNowDate());
        List<AppDeliveryManListVO> appDeliveryManListVOS = baseMapper.selectAppDeliveryManList(appDeliveryManListQueryDTO);
        for (AppDeliveryManListVO appDeliveryManListVO : appDeliveryManListVOS) {
            Long count = appDeliveryRelationService.count(new QueryWrapper<AppDeliveryRelation>().lambda()
                    .eq(AppDeliveryRelation::getAppUserId, appDeliveryManListVO.getAppUserId()));
            appDeliveryManListVO.setRelationStoreNum(count.intValue());
        }
        return appDeliveryManListVOS;
    }

    /**
     * 新增配送员
     *
     * @param appDeliveryManDTO 配送员
     * @return 结果
     */
    @Override
    public int insertAppDeliveryMan(AppDeliveryManDTO appDeliveryManDTO) {
        long count = this.count(new QueryWrapper<AppDeliveryMan>().lambda()
                .eq(AppDeliveryMan::getAppUserId, appDeliveryManDTO.getAppUserId()));
        if (count > 0) {
            throw new GlobalException("配送员已存在，无法添加");
        }
        if (CollUtil.isEmpty(appDeliveryManDTO.getRelationList())) {
            throw new GlobalException("配送员绑定门店为空，无法添加");
        }
        AppDeliveryMan appDeliveryMan = new AppDeliveryMan();
        BeanUtils.copyProperties(appDeliveryManDTO, appDeliveryMan);
        appDeliveryMan.buildCreateData();
        appDeliveryMan.setOrderCount(0);
        appDeliveryMan.setDelFlag(DeleteStatus.OK.getCode());
        appDeliveryMan.setStatus(StatusType.OK.getCode());
        baseMapper.insert(appDeliveryMan);

        List<AppDeliveryRelation> relationList = new ArrayList<>();
        for (AppDeliveryRelation appDeliveryRelation : appDeliveryManDTO.getRelationList()) {
            appDeliveryRelation.setAppUserId(appDeliveryManDTO.getAppUserId());
            appDeliveryRelation.buildCreateData();
            relationList.add(appDeliveryRelation);
        }
        appDeliveryRelationService.saveBatch(relationList);
        return 1;
    }

    /**
     * 修改配送员
     *
     * @param appDeliveryManDTO 配送员
     * @return 结果
     */
    @Override
    public int updateAppDeliveryMan(AppDeliveryManDTO appDeliveryManDTO) {
        AppDeliveryMan appDeliveryMan = new AppDeliveryMan();
        BeanUtils.copyProperties(appDeliveryManDTO, appDeliveryMan);
        appDeliveryMan.buildUpdateData();

        appDeliveryRelationService.remove(new QueryWrapper<AppDeliveryRelation>().lambda()
                .eq(AppDeliveryRelation::getAppUserId, appDeliveryManDTO.getAppUserId()));

        List<AppDeliveryRelation> relationList = new ArrayList<>();
        for (AppDeliveryRelation appDeliveryRelation : appDeliveryManDTO.getRelationList()) {
            appDeliveryRelation.setAppUserId(appDeliveryManDTO.getAppUserId());
            appDeliveryRelation.buildCreateData();
            relationList.add(appDeliveryRelation);
        }
        appDeliveryRelationService.saveBatch(relationList);
        return baseMapper.updateAppDeliveryMan(appDeliveryMan);
    }

    /**
     * 批量删除配送员
     *
     * @param ids 需要删除的配送员主键
     * @return 结果
     */
    @Override
    public int deleteAppDeliveryManByIds(Long[] ids) {
        return baseMapper.deleteAppDeliveryManByIds(ids);
    }

    /**
     * 删除配送员信息
     *
     * @param id 配送员主键
     * @return 结果
     */
    @Override
    public int deleteAppDeliveryManById(Long id) {
        return baseMapper.deleteAppDeliveryManById(id);
    }

    /**
     * 修改配送员状态
     *
     * @param appDeliveryMan 配送员
     * @return 结果
     */
    @Override
    public int updateAppDeliveryManStatus(AppDeliveryMan appDeliveryMan) {
        appDeliveryMan.setUpdateById(SecurityUtils.getUserId());
        appDeliveryMan.setUpdateBy(SecurityUtils.getUsername());
        appDeliveryMan.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateAppDeliveryMan(appDeliveryMan);
    }

    @Override
    public List<AppDeliveryManListVO> getDeliveryManListByOrg() {
        return baseMapper.getDeliveryManListByOrg(SecurityUtils.getLoginUser().getOrgId(), SecurityUtils.getLoginUser().getSourceType());
    }

    @Override
    public List<SysStore> getStoreList(Long deliveryId) {
        //查询配送员绑定的门店列表
        List<AppDeliveryRelation> list = appDeliveryRelationService.list(new QueryWrapper<AppDeliveryRelation>().lambda()
                .eq(AppDeliveryRelation::getAppUserId, deliveryId));
        if (CollUtil.isNotEmpty(list)) {
            List<Long> storeIds = list.stream().map(AppDeliveryRelation::getOrgId).collect(Collectors.toList());
            return sysStoreService.list(new QueryWrapper<SysStore>().lambda()
                    .in(SysStore::getId, storeIds));
        }
        return new ArrayList<>();
    }
}
