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

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

import com.ruoyi.common.core.domain.vo.LongCountVO;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.ss.storeStaff.service.StoreStaffValidator;
import com.ruoyi.ss.user.domain.SmUserVO;
import com.ruoyi.ss.user.service.ISmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ss.storeStaff.mapper.StoreStaffMapper;
import com.ruoyi.ss.storeStaff.domain.StoreStaff;
import com.ruoyi.ss.storeStaff.domain.StoreStaffVO;
import com.ruoyi.ss.storeStaff.domain.StoreStaffQuery;
import com.ruoyi.ss.storeStaff.service.StoreStaffService;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * 店铺员工Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-13
 */
@Service
public class StoreStaffServiceImpl implements StoreStaffService
{
    @Autowired
    private StoreStaffMapper storeStaffMapper;

    @Autowired
    private TransactionTemplate transactionTemplate;

    @Autowired
    private StoreStaffValidator storeStaffValidator;

    @Autowired
    private ISmUserService userService;

    /**
     * 查询店铺员工
     *
     * @param employId 店铺员工主键
     * @return 店铺员工
     */
    @Override
    public StoreStaffVO selectStoreStaffByEmployId(Long employId)
    {
        return storeStaffMapper.selectStoreStaffByEmployId(employId);
    }

    /**
     * 查询店铺员工列表
     *
     * @param storeStaff 店铺员工
     * @return 店铺员工
     */
    @Override
    public List<StoreStaffVO> selectStoreStaffList(StoreStaffQuery storeStaff)
    {
        return storeStaffMapper.selectStoreStaffList(storeStaff);
    }

    /**
     * 新增店铺员工
     *
     * @param storeStaff 店铺员工
     * @return 结果
     */
    @Override
    public int insertStoreStaff(StoreStaff storeStaff) {
        storeStaff.setCreateTime(DateUtils.getNowDate());

        Integer result = transactionTemplate.execute(status -> {
            int insert = storeStaffMapper.insertStoreStaff(storeStaff);
            ServiceUtil.assertion(insert != 1, "新增店铺员工失败");

            StoreStaffVO vo = this.selectStoreStaffByEmployId(storeStaff.getEmployId());
            storeStaffValidator.afterCheck(vo);

            return insert;
        });

        return result == null ? 0 : result;
    }

    /**
     * 修改店铺员工
     *
     * @param storeStaff 店铺员工
     * @return 结果
     */
    @Override
    public int updateStoreStaff(StoreStaff storeStaff) {
        Integer result = transactionTemplate.execute(status -> {
            int update = storeStaffMapper.updateStoreStaff(storeStaff);
            ServiceUtil.assertion(update != 1, "修改店铺员工失败");

            StoreStaffVO vo = this.selectStoreStaffByEmployId(storeStaff.getEmployId());
            storeStaffValidator.afterCheck(vo);

            return update;
        });

        return result == null ? 0 : result;
    }

    /**
     * 批量删除店铺员工
     *
     * @param employIds 需要删除的店铺员工主键
     * @return 结果
     */
    @Override
    public int deleteStoreStaffByEmployIds(List<Long> employIds)
    {
        return storeStaffMapper.deleteStoreStaffByEmployIds(employIds);
    }

    /**
     * 删除店铺员工信息
     *
     * @param employId 店铺员工主键
     * @return 结果
     */
    @Override
    public int deleteStoreStaffByEmployId(Long employId)
    {
        return storeStaffMapper.deleteStoreStaffByEmployId(employId);
    }

    @Override
    public BigDecimal selectSumOfPoint(StoreStaffQuery query) {
        return storeStaffMapper.selectSumOfPoint(query);
    }

    @Override
    public int selectCount(StoreStaffQuery query) {
        return storeStaffMapper.selectCount(query);
    }

    @Override
    public StoreStaffVO selectOne(StoreStaffQuery query) {
        List<StoreStaffVO> list = selectStoreStaffList(query);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public int bindUser(Long employId, Long userId) {
        if (employId == null || userId == null) {
            return 0;
        }
        StoreStaffVO old = selectStoreStaffByEmployId(employId);
        ServiceUtil.assertion(old == null, "员工信息不存在");
        ServiceUtil.assertion(old.getUserId() != null, "当前员工信息已被其他用户绑定，无法重复绑定");

        SmUserVO user = userService.selectSmUserByUserId(userId);
        ServiceUtil.assertion(user == null, "用户不存在");

        Integer result = transactionTemplate.execute(status -> {
            int bind = storeStaffMapper.bindUser(employId, userId);
            ServiceUtil.assertion(bind != 1, "绑定失败，当前员工信息已发生变化");

            // 后校验
            StoreStaffVO vo = selectStoreStaffByEmployId(employId);
            storeStaffValidator.checkRepeatUser(vo.getStoreId(), vo.getUserId(), vo.getEmployId());
            storeStaffValidator.checkPoint(vo);

            return bind;
        });

        return result == null ? 0 : result;
    }

    @Override
    public List<LongCountVO> selectCountGroupByUserId(StoreStaffQuery query) {
        return storeStaffMapper.selectCountGroupByUserId(query);
    }
}
