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

import java.util.Collections;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.ruoyi.bst.storeStaff.service.StoreStaffValidator;
import com.ruoyi.common.constant.RoleConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.system.role.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bst.storeStaff.mapper.StoreStaffMapper;
import com.ruoyi.bst.storeStaff.domain.StoreStaff;
import com.ruoyi.bst.storeStaff.domain.StoreStaffVO;
import com.ruoyi.bst.storeStaff.domain.StoreStaffQuery;
import com.ruoyi.bst.storeStaff.service.StoreStaffService;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * 员工运维Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-15
 */
@Service
@Slf4j
public class StoreStaffServiceImpl implements StoreStaffService
{
    @Autowired
    private StoreStaffMapper storeStaffMapper;
    @Autowired
    private TransactionTemplate transactionTemplate;
    @Autowired
    private RoleService roleService;
    @Autowired
    private StoreStaffValidator storeStaffValidator;

    /**
     * 查询员工运维
     *
     * @param id 员工运维主键
     * @return 员工运维
     */
    @Override
    public StoreStaffVO selectStoreStaffById(Long id,boolean scope)
    {
        if (id == null) {
            return null;
        }
        StoreStaffQuery query = new StoreStaffQuery();
        query.setId(id);
        query.setScope(scope);
        return this.selectOne(query);
    }

    @Override
    public StoreStaffVO selectOne(StoreStaffQuery query) {
        PageHelper.startPage(1, 1);
        List<StoreStaffVO> list = storeStaffMapper.selectStoreStaffList(query);
        return CollectionUtils.firstElement(list);
    }

    /**
     * 查询员工运维列表
     *
     * @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 rows = storeStaffMapper.insertStoreStaff(storeStaff);

            if (rows > 0) {
                // 尝试修改用户角色
                this.tryUpdateUserRole(storeStaff);

                // 后校验
               // storeStaffValidator.afterCheck(storeStaff.getId());
            }

            return rows;
        });

        return result == null ? 0 : result;
    }

    /**
     * 尝试修改用户角色
     *
     * @param storeStaff
     */
    private void tryUpdateUserRole(StoreStaff storeStaff) {
        try {
            roleService.insertAuthUsers(RoleConstants.STAFF, Collections.singletonList(storeStaff.getUserId()));
        } catch (Exception e) {
            log.warn("尝试修改ID为{}的用户角色失败:{}", storeStaff.getUserId(), e);
        }
    }

    /**
     * 修改员工运维
     *
     * @param storeStaff 员工运维
     * @return 结果
     */
    @Override
    public int updateStoreStaff(StoreStaff storeStaff)
    {
        return storeStaffMapper.updateStoreStaff(storeStaff);
    }

    /**
     * 批量删除员工运维
     *
     * @param ids 需要删除的员工运维主键
     * @return 结果
     */
    @Override
    public int deleteStoreStaffByIds(Long[] ids)
    {
        return storeStaffMapper.deleteStoreStaffByIds(ids);
    }

    /**
     * 删除员工运维信息
     *
     * @param id 员工运维主键
     * @return 结果
     */
    @Override
    public int deleteStoreStaffById(Long id)
    {
        return storeStaffMapper.deleteStoreStaffById(id);
    }
}
