package com.younike.system.service.impl;

import com.younike.common.core.domain.AjaxResult;
import com.younike.common.exception.ServiceException;
import com.younike.common.utils.SecurityUtils;
import com.younike.system.domain.*;
import com.younike.system.domain.vo.*;
import com.younike.system.mapper.GoodsServerMapper;
import com.younike.system.mapper.MerchantServerTimeMapper;
import com.younike.system.mapper.ServerUserMapper;
import com.younike.system.param.ServerUserQueryParam;
import com.younike.system.service.IGoodsService;
import com.younike.system.service.IMerchantService;
import com.younike.system.service.IOrderService;
import com.younike.system.service.IServerUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 服务人员Service业务层处理
 *
 * @author elev
 * @date 2022-01-06
 */
@Service
public class ServerUserServiceImpl implements IServerUserService {
    @Autowired
    private ServerUserMapper serverUserMapper;
    @Autowired
    private MerchantServerTimeMapper serverTimeMapper;
    @Autowired
    private GoodsServerMapper goodsServerMapper;
    @Autowired
    private IMerchantService merchantService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IGoodsService goodsService;

    /**
     * 查询商家服务人员列表
     *
     * @param param
     * @return
     */
    @Override
    public List<ServerUserListVo> pageMerchantServerUserList(ServerUserQueryParam param) {
        List<ServerUserListVo> serverUserListVos = serverUserMapper.adminMerchantServerUserList(param);
        if (CollectionUtils.isNotEmpty(serverUserListVos)) {
            for (ServerUserListVo serverUserListVo : serverUserListVos) {
                List<GoodsServerResultVo> goodsServerResultVoList = goodsServerMapper.serverGoodsList(serverUserListVo.getServerId());
                serverUserListVo.setGoodsCount(CollectionUtils.isEmpty(goodsServerResultVoList) ? 0 : goodsServerResultVoList.size());
            }
        }
        return serverUserListVos;
    }

    /**
     * 查询个体户服务人员列表
     *
     * @param param
     * @return
     */
    @Override
    public List<ServerUserListVo> pageList(ServerUserQueryParam param) {
        List<ServerUserListVo> serverUserListVos = serverUserMapper.adminUserList(param);
        if (CollectionUtils.isNotEmpty(serverUserListVos)) {
            for (ServerUserListVo serverUserListVo : serverUserListVos) {
                List<GoodsServerResultVo> goodsServerResultVoList = goodsServerMapper.serverGoodsList(serverUserListVo.getServerId());
                serverUserListVo.setGoodsCount(CollectionUtils.isEmpty(goodsServerResultVoList) ? 0 : goodsServerResultVoList.size());
            }
        }
        return serverUserListVos;
    }

    @Override
    public List<ServerUserListVo> pageMerchantList(ServerUserQueryParam param) {
        Merchant merchant = merchantService.selectMerchantByAdminId(SecurityUtils.getUserId());
        if (Objects.isNull(merchant)){
            throw new ServiceException("非平台入驻商户，查询失败");
        }
        param.setMerchantId(merchant.getMerchantId().toString());
        List<ServerUserListVo> serverUserListVos = serverUserMapper.merchantUserList(param);
        if (CollectionUtils.isNotEmpty(serverUserListVos)) {
            for (ServerUserListVo serverUserListVo : serverUserListVos) {
                List<GoodsServerResultVo> goodsServerResultVoList = goodsServerMapper.serverGoodsList(serverUserListVo.getServerId());
                serverUserListVo.setGoodsCount(CollectionUtils.isEmpty(goodsServerResultVoList) ? 0 : goodsServerResultVoList.size());
            }
        }
        return serverUserListVos;
    }

    /**
     * 店铺管理
     * 删除服务人员
     *
     * @param serverIds
     * @return
     */
    @Override
    public AjaxResult merchantServerUser(Long[] serverIds) {

        //根据服务人员id查询服务人员是否有未完成的订单
        List<Order> orderList = orderService.merchanServerUserId(serverIds);

        if (orderList.size() > 0) {
            return AjaxResult.error("服务人员有未完成订单，不可删除服务人员");
        }
        //查询服务人员是否有关联的上架的服务（如果有，校验服务是否只有这一个要删除的服务人员）
        List<GoodsVo> goodsList = goodsService.merchanServerUserId(serverIds);
        if (goodsList.size() > 0) {
            for (GoodsVo vo : goodsList) {
                GoodsFormVO detail = goodsService.detail(vo.getGoodsId());
                if (detail.getServerUserList().size() < 2) {
                    return AjaxResult.error("服务人员关联服务处于上架状态，不可删除服务人员");
                }
            }
        }
        //删除服务人员
        if (this.deleteServerUserByServerIds(serverIds) < 0) {
            return AjaxResult.error("删除服务人员失败，请稍后重试！");
        }

        return AjaxResult.success("删除成功");
    }

    /**
     * 查询服务人员
     *
     * @param serverId 服务人员主键
     * @return 服务人员
     */
    @Override
    public ServerUser selectServerUserByServerId(Long serverId) {
        ServerUser serverUser = serverUserMapper.selectServerUserByServerId(serverId);
        if (Objects.isNull(serverUser)) {
            throw new ServiceException("服务人员不存在或已被删除");
        }
        return serverUser;
    }

    /**
     * 查询服务人员列表
     *
     * @param serverUser 服务人员
     * @return 服务人员
     */
    @Override
    public List<ServerUser> selectServerUserList(ServerUser serverUser) {
        return serverUserMapper.selectServerUserList(serverUser);
    }

    @Override
    public List<ServerUser> getServerUserList(ServerUserSearchParam searchParam) {
        return serverUserMapper.getServerUserList(searchParam);
    }

    /**
     * 新增服务人员
     *
     * @param serverUser 服务人员
     * @return 结果
     */
    @Override
    public int insertServerUser(ServerUser serverUser) {
        serverUser.setCreateTime(LocalDateTime.now());
        ServerTimesVO serverTime = new ServerTimesVO();
        if (serverUserMapper.existsPhone(serverUser)){
            throw new ServiceException("该手机号已被注册");
        }

        serverTime.setStopOrder(1);
        serverTime.setDelFlag("0");
        serverTime.setUpdateTime(LocalDateTime.now());
        serverTime.setCreateTime(LocalDateTime.now());
        serverUser.setNikeName(serverUser.getRealName());
        int i =serverUserMapper.insertServerUser(serverUser);
        serverTime.setServerId(serverUserMapper.findByPhone(serverUser.getPhone()).getServerId());
        serverTimeMapper.insertServerTimeCustom(serverTime);
        return i ;
    }

    /**
     * 修改服务人员
     *
     * @param serverUser 服务人员
     * @return 结果
     */
    @Override
    public int updateServerUser(ServerUser serverUser) {
        if (serverUserMapper.existsPhone(serverUser)){
            throw new ServiceException("该手机号已被注册");
        }
        serverUser.setUpdateTime(LocalDateTime.now());
        return serverUserMapper.updateServerUser(serverUser);
    }

    /**
     * 批量删除服务人员
     *
     * @param serverIds 需要删除的服务人员主键
     * @return 结果
     */
    @Override
    public int deleteServerUserByServerIds(Long[] serverIds) {
        return serverUserMapper.deleteServerUserByServerIds(serverIds);
    }

    /**
     * 删除服务人员信息
     *
     * @param serverId 服务人员主键
     * @return 结果
     */
    @Override
    public int deleteServerUserByServerId(Long serverId)
    {
        return serverUserMapper.deleteServerUserByServerId(serverId);
    }

    @Override
    public ServerUser findByPhone(String phone) {
        return serverUserMapper.findByPhone(phone);
    }
    @Override
    public ServerUser findByOpenid(String openid) {
        return serverUserMapper.findByOpenid(openid);
    }

    /**
     * 查询服务人员数量
     *
     * @return
     */
    @Override
    public int getServerUserCount() {
        return serverUserMapper.getServerUserCount();
    }

    /**
     * 根据服务人员id查询服务人员数据
     * @param serverUserId
     * @return
     */
    @Override
    public ServerUserVo selectUserTime(Long serverUserId) {
        return serverUserMapper.serverUserTime(serverUserId);
    }

    /*
     * 查询服务人员下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<ServerUserListVo> pageDownSelectList(ServerUserQueryParam param) {
        List<ServerUserListVo> serverUserListVos = serverUserMapper.pageDownSelectList(param);
        if (CollectionUtils.isNotEmpty(serverUserListVos)) {
            for (ServerUserListVo serverUserListVo : serverUserListVos) {
                List<GoodsServerResultVo> goodsServerResultVoList = goodsServerMapper.serverGoodsList(serverUserListVo.getServerId());
                serverUserListVo.setGoodsCount(CollectionUtils.isEmpty(goodsServerResultVoList) ? 0 : goodsServerResultVoList.size());
            }
        }
        return serverUserListVos;
    }

    /**
     * 根据serverId查询服务人员信息
     *
     * @param serverId
     * @return
     */
    @Override
    public ServerUserListVo getByServerId(Long serverId) {
        return serverUserMapper.getByServerId(serverId);
    }

    @Override
    public ServerUserVo selectUserInfo(ServerUser serverUser) {
        return serverUserMapper.selectUserInfo(serverUser);
    }

    /**
     * 获取服务人员工作时间
     * @param serverId
     * @return
     */
    @Override
    public ServerTime getServerTime(Long serverId) {
        return null;
    }

    /**
     * 批量更新
     *
     * @param users 服务人员
     */
    @Override
    public void updateServerUsers(List<ServerUser> users) {
        serverUserMapper.updateServerUsers(users);
    }
}
