package com.bonus.canteen.core.kitchen.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollUtil;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.canteen.core.kitchen.mapper.KitchenStaffDevicePrivilegeMapper;
import com.bonus.canteen.core.kitchen.domain.KitchenStaffDevicePrivilege;
import com.bonus.canteen.core.kitchen.service.IKitchenStaffDevicePrivilegeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 厨房员工设备权限Service业务层处理
 * 
 * @author xsheng
 * @date 2025-06-17
 */
@Service
public class KitchenStaffDevicePrivilegeServiceImpl implements IKitchenStaffDevicePrivilegeService {
    @Autowired
    private KitchenStaffDevicePrivilegeMapper kitchenStaffDevicePrivilegeMapper;

    /**
     * 查询厨房员工设备权限
     * 
     * @param id 厨房员工设备权限主键
     * @return 厨房员工设备权限
     */
    @Override
    public KitchenStaffDevicePrivilege selectKitchenStaffDevicePrivilegeById(Long id) {
        return kitchenStaffDevicePrivilegeMapper.selectKitchenStaffDevicePrivilegeById(id);
    }

    /**
     * 查询厨房员工设备权限列表
     * 
     * @param kitchenStaffDevicePrivilege 厨房员工设备权限
     * @return 厨房员工设备权限
     */
    @Override
    public List<KitchenStaffDevicePrivilege> selectKitchenStaffDevicePrivilegeList(KitchenStaffDevicePrivilege kitchenStaffDevicePrivilege) {
        return kitchenStaffDevicePrivilegeMapper.selectKitchenStaffDevicePrivilegeList(kitchenStaffDevicePrivilege);
    }

    /**
     * 新增厨房员工设备权限
     * 
     * @param kitchenStaffDevicePrivilege 厨房员工设备权限
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertKitchenStaffDevicePrivilege(List<KitchenStaffDevicePrivilege> kitchenStaffDevicePrivilege) {
        if(CollUtil.isNotEmpty(kitchenStaffDevicePrivilege)) {
            for (KitchenStaffDevicePrivilege privilege : kitchenStaffDevicePrivilege) {
                privilege.setCreateTime(DateUtils.getNowDate());
                privilege.setCreateBy(SecurityUtils.getUsername());
                try {
                    kitchenStaffDevicePrivilegeMapper.insertKitchenStaffDevicePrivilege(privilege);
                } catch (Exception e) {
                    throw new ServiceException(e.getMessage());
                }
            }
        }
         return 0;
    }

    /**
     * 修改厨房员工设备权限
     * 
     * @param kitchenStaffDevicePrivilege 厨房员工设备权限
     * @return 结果
     */
    @Override
    public int updateKitchenStaffDevicePrivilege(List<KitchenStaffDevicePrivilege> kitchenStaffDevicePrivilege) {
        if(CollUtil.isNotEmpty(kitchenStaffDevicePrivilege)) {
            for (KitchenStaffDevicePrivilege privilege : kitchenStaffDevicePrivilege) {
                privilege.setUpdateTime(DateUtils.getNowDate());
                privilege.setUpdateBy(SecurityUtils.getUsername());
                try {
                    return kitchenStaffDevicePrivilegeMapper.updateKitchenStaffDevicePrivilege(privilege);
                } catch (Exception e) {
                    throw new ServiceException(e.getMessage());
                }
            }
        }
        return 0;
    }

    /**
     * 批量删除厨房员工设备权限
     * 
     * @param ids 需要删除的厨房员工设备权限主键
     * @return 结果
     */
    @Override
    public int deleteKitchenStaffDevicePrivilegeByIds(Long[] ids) {
        return kitchenStaffDevicePrivilegeMapper.deleteKitchenStaffDevicePrivilegeByIds(ids);
    }

    /**
     * 删除厨房员工设备权限信息
     * 
     * @param id 厨房员工设备权限主键
     * @return 结果
     */
    @Override
    public int deleteKitchenStaffDevicePrivilegeById(Long id) {
        return kitchenStaffDevicePrivilegeMapper.deleteKitchenStaffDevicePrivilegeById(id);
    }
}
