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

import java.util.*;

import cn.hutool.core.collection.CollUtil;
import com.bonus.canteen.core.kitchen.domain.AccessAuthority;
import com.bonus.canteen.core.kitchen.domain.KitchenDeviceInfo;
import com.bonus.canteen.core.kitchen.domain.KitchenStaffDevicePrivilege;
import com.bonus.canteen.core.kitchen.domain.constants.HealthCertStatusEnum;
import com.bonus.canteen.core.kitchen.mapper.KitchenDeviceInfoMapper;
import com.bonus.canteen.core.kitchen.mapper.KitchenStaffDevicePrivilegeMapper;
import com.bonus.canteen.core.kitchen.service.IKitchenStaffDevicePrivilegeService;
import com.bonus.canteen.core.kitchen.vo.KitchenDeviceListVO;
import com.bonus.canteen.core.user.domain.UserFace;
import com.bonus.canteen.core.user.service.IUserFaceService;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.houqin.constant.DeviceTypeEnum;
import com.bonus.common.houqin.utils.SM4EncryptUtils;
import com.bonus.common.security.utils.SecurityUtils;
import org.apache.catalina.security.SecurityUtil;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.canteen.core.kitchen.mapper.KitchenStaffInfoMapper;
import com.bonus.canteen.core.kitchen.domain.KitchenStaffInfo;
import com.bonus.canteen.core.kitchen.service.IKitchenStaffInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 厨房员工资料Service业务层处理
 *
 * @author xsheng
 * @date 2025-06-16
 */
@Service
public class KitchenStaffInfoServiceImpl implements IKitchenStaffInfoService {
    @Autowired
    private KitchenStaffInfoMapper kitchenStaffInfoMapper;
    @Autowired
    private IKitchenStaffDevicePrivilegeService kitchenStaffDevicePrivilegeService;
    @Autowired
    private KitchenStaffDevicePrivilegeMapper kitchenStaffDevicePrivilegeMapper;
    @Autowired
    private KitchenDeviceInfoMapper kitchenDeviceInfoMapper;

    /**
     * 查询厨房员工资料
     *
     * @param staffId 厨房员工资料主键
     * @return 厨房员工资料
     */
    @Override
    public KitchenStaffInfo selectKitchenStaffInfoByStaffId(Long staffId) {
        KitchenStaffInfo kitchenStaffInfo = kitchenStaffInfoMapper.selectKitchenStaffInfoByStaffId(staffId);
        if(Objects.nonNull(kitchenStaffInfo)) {
            kitchenStaffInfo.setPhonenumber(SM4EncryptUtils.sm4Decrypt(kitchenStaffInfo.getPhonenumber()));
            List<KitchenStaffDevicePrivilege> kitchenStaffDevicePrivilegeList = kitchenStaffDevicePrivilegeMapper
                    .selectKitchenStaffDevicePrivilegeByStaffId(staffId);
            if(CollUtil.isNotEmpty(kitchenStaffDevicePrivilegeList)) {
                List<AccessAuthority> accessAuthorityList = Lists.newArrayList();
                AccessAuthority accessAuthority = new AccessAuthority();
                for (KitchenStaffDevicePrivilege kitchenStaffDevicePrivilege : kitchenStaffDevicePrivilegeList) {
                    KitchenDeviceListVO deviceInfo = kitchenDeviceInfoMapper.selectKitchenDeviceInfoByDeviceId(kitchenStaffDevicePrivilege.getDeviceId());
                    if(Objects.nonNull(deviceInfo)) {
                        accessAuthority.setDeviceName(deviceInfo.getDeviceName());
                        accessAuthority.setLocation(deviceInfo.getSubPlace());
                    }
                    accessAuthority.setDeviceId(kitchenStaffDevicePrivilege.getDeviceId());
                    accessAuthority.setDeviceType(kitchenStaffDevicePrivilege.getDeviceType());
                    accessAuthority.setPrivilegeValue(kitchenStaffDevicePrivilege.getPrivilegeValue());
                    accessAuthorityList.add(accessAuthority);
                }
                kitchenStaffInfo.setAccessAuthorityList(accessAuthorityList);
            }
        }
        return kitchenStaffInfo;
    }

    /**
     * 查询厨房员工资料列表
     *
     * @param kitchenStaffInfo 厨房员工资料
     * @return 厨房员工资料
     */
    @Override
    public List<KitchenStaffInfo> selectKitchenStaffInfoList(KitchenStaffInfo kitchenStaffInfo) {
        String encryptedSearchValue = SM4EncryptUtils.sm4Encrypt(kitchenStaffInfo.getSearchValue());
        kitchenStaffInfo.setEncryptedSearchValue(encryptedSearchValue);
        List<KitchenStaffInfo> kitchenStaffInfoList = kitchenStaffInfoMapper.selectKitchenStaffInfoList(kitchenStaffInfo);
        if(CollUtil.isNotEmpty(kitchenStaffInfoList)) {
            for (KitchenStaffInfo info : kitchenStaffInfoList) {
                info.setPhonenumber(SM4EncryptUtils.sm4Decrypt(info.getPhonenumber()));
                info.setHealthCertStatusName(HealthCertStatusEnum.getDescByKey(info.getHealthCertStatus()));
            }
        }
         return kitchenStaffInfoList;
    }

    /**
     * 新增厨房员工资料
     *
     * @param kitchenStaffInfo 厨房员工资料
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertKitchenStaffInfo(KitchenStaffInfo kitchenStaffInfo) {
        kitchenStaffInfo.setCreateTime(DateUtils.getNowDate());
        kitchenStaffInfo.setCreateBy(SecurityUtils.getUsername());
        kitchenStaffInfo.setStaffId(null);
        try {
            checkUser(kitchenStaffInfo);
            int result = kitchenStaffInfoMapper.insertKitchenStaffInfo(kitchenStaffInfo);
            addPrivileges(kitchenStaffInfo);
            return result;
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 修改厨房员工资料
     *
     * @param kitchenStaffInfo 厨房员工资料
     * @return 结果
     */
    @Override
    public int updateKitchenStaffInfo(KitchenStaffInfo kitchenStaffInfo) {
        kitchenStaffInfo.setUpdateTime(DateUtils.getNowDate());
        kitchenStaffInfo.setUpdateBy(SecurityUtils.getUsername());
        if(Objects.isNull(kitchenStaffInfo.getStaffId())) {
            throw new ServiceException("员工编号不能为空");
        }
        try {
            checkUser(kitchenStaffInfo);
            kitchenStaffInfoMapper.updateKitchenStaffInfo(kitchenStaffInfo);
            kitchenStaffDevicePrivilegeMapper.deleteKitchenStaffDevicePrivilegeByStaffId(kitchenStaffInfo.getStaffId());
            addPrivileges(kitchenStaffInfo);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
        return 1;
    }

    private void checkUser(KitchenStaffInfo kitchenStaffInfo) {
        KitchenStaffInfo kitchenStaffInfoByPhone = new KitchenStaffInfo();
        kitchenStaffInfoByPhone.setUserId(kitchenStaffInfo.getUserId());
        List<KitchenStaffInfo> staffInfoList = kitchenStaffInfoMapper.selectKitchenStaffInfoList(kitchenStaffInfoByPhone);
        if(CollUtil.isNotEmpty(staffInfoList) && !Objects.equals(staffInfoList.get(0).getStaffId(), kitchenStaffInfo.getStaffId())) {
            throw new ServiceException("该用户已存在");
        }
        kitchenStaffInfoByPhone = new KitchenStaffInfo();
        kitchenStaffInfoByPhone.setStaffNo(kitchenStaffInfo.getStaffNo());
        staffInfoList = kitchenStaffInfoMapper.selectKitchenStaffInfoList(kitchenStaffInfoByPhone);
        if(CollUtil.isNotEmpty(staffInfoList) && !Objects.equals(staffInfoList.get(0).getStaffId(), kitchenStaffInfo.getStaffId())) {
            throw new ServiceException("该用户编号已存在");
        }
    }

    private void addPrivileges(KitchenStaffInfo kitchenStaffInfo) {
        if(CollUtil.isNotEmpty(kitchenStaffInfo.getAccessAuthorityList())) {
            List<KitchenStaffDevicePrivilege> kitchenStaffDevicePrivilegeList = new ArrayList<>();
            for(AccessAuthority access : kitchenStaffInfo.getAccessAuthorityList()) {
                KitchenStaffDevicePrivilege kitchenStaffDevicePrivilege = new KitchenStaffDevicePrivilege();
                kitchenStaffDevicePrivilege.setStaffId(kitchenStaffInfo.getStaffId());
                kitchenStaffDevicePrivilege.setDeviceId(access.getDeviceId());
                kitchenStaffDevicePrivilege.setDeviceType(access.getDeviceType());
                kitchenStaffDevicePrivilege.setPrivilegeName("is_enable");
                kitchenStaffDevicePrivilege.setPrivilegeValue(access.getPrivilegeValue());
                kitchenStaffDevicePrivilegeList.add(kitchenStaffDevicePrivilege);
            }
            kitchenStaffDevicePrivilegeService.insertKitchenStaffDevicePrivilege(kitchenStaffDevicePrivilegeList);
        }
    }

    /**
     * 批量删除厨房员工资料
     *
     * @param staffIds 需要删除的厨房员工资料主键
     * @return 结果
     */
    @Override
    public int deleteKitchenStaffInfoByStaffIds(Long[] staffIds) {
        kitchenStaffDevicePrivilegeMapper.deleteKitchenStaffDevicePrivilegeByStaffIds(staffIds);
        return kitchenStaffInfoMapper.deleteKitchenStaffInfoByStaffIds(staffIds);
    }

    /**
     * 删除厨房员工资料信息
     *
     * @param staffId 厨房员工资料主键
     * @return 结果
     */
    @Override
    public int deleteKitchenStaffInfoByStaffId(Long staffId) {
        kitchenStaffDevicePrivilegeMapper.deleteKitchenStaffDevicePrivilegeByStaffId(staffId);
        return kitchenStaffInfoMapper.deleteKitchenStaffInfoByStaffId(staffId);
    }
}
