package com.ruoyi.ss.storeStaff.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.ss.device.domain.vo.DeviceVO;
import com.ruoyi.ss.device.service.DeviceAssembler;
import com.ruoyi.ss.device.service.DeviceService;
import com.ruoyi.ss.device.service.DeviceValidator;
import com.ruoyi.ss.store.domain.StoreVo;
import com.ruoyi.ss.storeStaff.domain.StoreStaff;
import com.ruoyi.ss.storeStaff.domain.StoreStaffQuery;
import com.ruoyi.ss.storeStaff.domain.StoreStaffVO;
import com.ruoyi.ss.storeStaff.domain.enums.StoreStaffPermissions;
import com.ruoyi.ss.storeStaff.service.StoreStaffService;
import com.ruoyi.ss.storeStaff.service.StoreStaffValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author wjh
 * 2024/11/13
 */
@Service
public class StoreStaffValidatorImpl implements StoreStaffValidator {

    @Autowired
    private DeviceAssembler deviceAssembler;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private DeviceValidator deviceValidator;

    @Autowired
    private StoreStaffService storeStaffService;

    @Override
    public void checkPoint(StoreStaff data) {
        if (data == null) {
            return;
        }

        List<DeviceVO> deviceList = deviceService.selectSmDeviceByStoreId(data.getStoreId());
        if (CollectionUtils.isEmptyElement(deviceList)) {
            return;
        }
        deviceAssembler.assembleBonusList(deviceList);

        // 校验设备分成
        for (DeviceVO device : deviceList) {
            try {
                deviceValidator.checkBonus(device);
            } catch (Exception e) {
                throw new ServiceException("校验分成比例失败：" + e.getMessage() + "，请修改分成比例");
            }
        }
    }

    @Override
    public void afterCheck(StoreStaffVO vo) {
        if (vo == null) {
            return;
        }
        // 校验用户是否重复成为店铺员工
        this.checkRepeatUser(vo.getStoreId(), vo.getUserId(), vo.getEmployId());
        // 校验分成
        this.checkPoint(vo);
    }

    @Override
    public boolean isMch(List<Long> employIds, Long mchId) {
        if (mchId == null) {
            return false;
        }
        if (CollectionUtils.isEmptyElement(employIds)) {
            return true;
        }
        StoreStaffQuery query = new StoreStaffQuery();
        query.setEmployIds(employIds);
        query.setMchId(mchId);
        List<StoreStaffVO> list = storeStaffService.selectStoreStaffList(query);

        for (Long employId : employIds) {
            if (employId == null) {
                continue;
            }
            StoreStaffVO staff = list.stream().filter(item -> Objects.equals(employId, item.getEmployId()) && Objects.equals(item.getMchId(), mchId))
                    .findFirst()
                    .orElse(null);
            if (staff == null) {
                return false;
            }
        }

        return true;
    }

    @Override
    public boolean hasStorePermission(Long storeId, Long userId, StoreStaffPermissions permission) {
        if (storeId == null || userId == null || permission == null) {
            return false;
        }
        StoreStaffQuery query = new StoreStaffQuery();
        query.setStoreId(storeId);
        query.setUserId(userId);
        query.setEnabled(true);
        query.setPermission(permission.getCode());
        return storeStaffService.selectCount(query) > 0;
    }

    @Override
    public void checkRepeatUser(Long storeId, Long userId, Long employId) {
        if (storeId == null || userId == null) {
            return;
        }
        StoreStaffQuery query = new StoreStaffQuery();
        query.setStoreId(storeId);
        query.setUserId(userId);
        query.setExcludeEmployId(employId);
        ServiceUtil.assertion(storeStaffService.selectCount(query) > 0, "当前用户已成为店铺员工，请勿重复操作");
    }
}
