package cn.iocoder.yudao.module.system.service.facedeviceinfo;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.facedeviceinfo.vo.FaceDeviceInfoPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.facedeviceinfo.vo.FaceDeviceInfoSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.facedeviceinfo.FaceDeviceInfoDO;
import cn.iocoder.yudao.module.system.dal.mysql.carteen.CarteenMapper;
import cn.iocoder.yudao.module.system.dal.mysql.facedeviceinfo.FaceDeviceInfoMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.FACE_DEVICE_INFO_EXISTS;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.FACE_DEVICE_INFO_NOT_EXISTS;


/**
 * 人脸设备信息关联门店 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class FaceDeviceInfoServiceImpl implements FaceDeviceInfoService {

    @Resource
    private FaceDeviceInfoMapper faceDeviceInfoMapper;

    @Resource
    private CarteenMapper carteenMapper;

    @Override
    public Long createFaceDeviceInfo(FaceDeviceInfoSaveReqVO createReqVO) {
        // 插入
        FaceDeviceInfoDO faceDeviceInfo = BeanUtils.toBean(createReqVO, FaceDeviceInfoDO.class);
        List<FaceDeviceInfoDO> faceDeviceInfoDOS = faceDeviceInfoMapper.selectList(new LambdaQueryWrapper<FaceDeviceInfoDO>().eq(FaceDeviceInfoDO::getMac, createReqVO.getMac()));
        if (CollectionUtil.isNotEmpty(faceDeviceInfoDOS)){
            throw exception(FACE_DEVICE_INFO_EXISTS);
        }

        faceDeviceInfoMapper.insert(faceDeviceInfo);
        // 返回
        return faceDeviceInfo.getId();
    }

    @Override
    public void updateFaceDeviceInfo(FaceDeviceInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateFaceDeviceInfoExists(updateReqVO.getId());

        List<FaceDeviceInfoDO> faceDeviceInfoDOS = faceDeviceInfoMapper.selectList(new LambdaQueryWrapper<FaceDeviceInfoDO>()
                .eq(FaceDeviceInfoDO::getMac, updateReqVO.getMac())
                .ne(FaceDeviceInfoDO::getId, updateReqVO.getId()));
        if (CollectionUtil.isNotEmpty(faceDeviceInfoDOS)){
            throw exception(FACE_DEVICE_INFO_EXISTS);
        }
        // 更新
        FaceDeviceInfoDO updateObj = BeanUtils.toBean(updateReqVO, FaceDeviceInfoDO.class);
        faceDeviceInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteFaceDeviceInfo(Long id) {
        // 校验存在
        validateFaceDeviceInfoExists(id);
        // 删除
        faceDeviceInfoMapper.deleteById(id);
    }

    private void validateFaceDeviceInfoExists(Long id) {
        if (faceDeviceInfoMapper.selectById(id) == null) {
            throw exception(FACE_DEVICE_INFO_NOT_EXISTS);
        }
    }

    @Override
    public FaceDeviceInfoDO getFaceDeviceInfo(Long id) {
        return faceDeviceInfoMapper.selectById(id);
    }

    @Override
    public PageResult<FaceDeviceInfoDO> getFaceDeviceInfoPage(FaceDeviceInfoPageReqVO pageReqVO) {
        PageResult<FaceDeviceInfoDO> faceDeviceInfoDOPageResult = faceDeviceInfoMapper.selectPage(pageReqVO);
        List<FaceDeviceInfoDO> list = faceDeviceInfoDOPageResult.getList();
        if (CollectionUtil.isNotEmpty(list)){
            list.forEach(vo ->{
                if(vo.getCarteenId()!=null){
                    vo.setCarteenName(carteenMapper.selectById(vo.getCarteenId()).getStoresName());
                }
            });
        }
        return faceDeviceInfoDOPageResult;
    }

    @Override
    public FaceDeviceInfoDO getInfo(String mac) {
        LambdaQueryWrapper<FaceDeviceInfoDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FaceDeviceInfoDO::getMac, mac);
        FaceDeviceInfoDO faceDeviceInfoDO = faceDeviceInfoMapper.selectOne(wrapper);
        if(faceDeviceInfoDO.getCarteenId() != null){
            faceDeviceInfoDO.setCarteenName(carteenMapper.selectById(faceDeviceInfoDO.getCarteenId()).getStoresName());
        }
        return faceDeviceInfoDO;
    }
}