package com.yc.ship.module.resource.service.machine;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yc.ship.framework.common.lang.Asserts;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.security.core.LoginUser;
import com.yc.ship.framework.security.core.util.SecurityFrameworkUtils;
import com.yc.ship.framework.tenant.core.aop.TenantIgnore;
import com.yc.ship.module.resource.api.dto.CheckMachineDTO;
import com.yc.ship.module.resource.controller.admin.machine.vo.ScenicMachinePageReqVO;
import com.yc.ship.module.resource.controller.admin.machine.vo.ScenicMachineSaveReqVO;
import com.yc.ship.module.resource.dal.dataobject.machine.ScenicMachineDO;
import com.yc.ship.module.resource.dal.mysql.machine.ScenicMachineMapper;
import com.yc.ship.module.resource.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;


/**
 * 设备 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class ScenicMachineServiceImpl implements ScenicMachineService {

    @Resource
    private ScenicMachineMapper scenicMachineMapper;


    @Override
    public Long createScenicMachine(ScenicMachineSaveReqVO createReqVO) {
        // 插入
        ScenicMachineDO scenicMachine = BeanUtils.toBean(createReqVO, ScenicMachineDO.class);
        Long id = IdWorker.getId(scenicMachine);
        scenicMachine.setId(id);
        scenicMachine.setDeleted(false);
        scenicMachineMapper.insert(scenicMachine);
        // 返回
        return scenicMachine.getId();
    }

    @Override
    public void updateScenicMachine(ScenicMachineSaveReqVO updateReqVO) {
        // 校验存在
        validateScenicMachineExists(updateReqVO.getId());
        // 更新
        ScenicMachineDO updateObj = BeanUtils.toBean(updateReqVO, ScenicMachineDO.class);
        scenicMachineMapper.updateById(updateObj);
    }

    @Override
    public void deleteScenicMachine(Long id) {
        // 校验存在
        validateScenicMachineExists(id);
        // 删除
        scenicMachineMapper.deleteById(id);
    }

    private void validateScenicMachineExists(Long id) {
        if (scenicMachineMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.SCENIC_MACHINE_NOT_EXISTS);
        }
    }

    @Override
    public ScenicMachineDO getScenicMachine(Long id) {
        return scenicMachineMapper.selectById(id);
    }

    @Override
    public PageResult<ScenicMachineDO> getScenicMachinePage(ScenicMachinePageReqVO pageReqVO) {
        return scenicMachineMapper.selectPage(pageReqVO);
    }

    @Override
    @TenantIgnore
    public CheckMachineDTO getCheckMachine(String machineId, Integer type, Long tenantId) {
        return scenicMachineMapper.getCheckMachine(machineId, type, tenantId);
    }

    @Override
    public List<Map<String, Object>> getMachineSiteInfos(String machineId, Integer siteType, Long tenantId) {
        return scenicMachineMapper.getMachineSiteInfos(machineId, siteType, tenantId);
    }

    @Override
    public CommonResult<?> changeCheckSiteChannel(Integer machineType, Long channelId, String machineId) {


        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        Long tenantId = null;
        if (loginUser != null) {
            tenantId = loginUser.getTenantId();
        }
        List<Map<String, Object>> mapList = this.getMachineSiteInfos(machineId, 1, tenantId);
        Asserts.isTrue(CollUtil.isNotEmpty(mapList), "设备编号不存在");
        Long id = MapUtil.getLong(mapList.get(0), "id");
        ScenicMachineDO scenicMachineDO = this.getScenicMachine(id);
        if(scenicMachineDO.getIsModify()!=null && scenicMachineDO.getIsModify()==0){
            return CommonResult.error(ErrorCodeConstants.MACHINE_MODIFY_EXCEPTION);
        }
        scenicMachineDO.setChannelId(channelId);
        scenicMachineDO.setMachineType(machineType);
        scenicMachineMapper.updateById(scenicMachineDO);
        return CommonResult.success(id);

    }

    @Override
    public Map<String, Object> getMachineInfo(Integer siteType,String machineId) {

        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        Long tenantId = null;
        if (loginUser != null) {
            tenantId = loginUser.getTenantId();
        }
        List<Map<String, Object>> mapList = this.getMachineSiteInfos(machineId, siteType, tenantId);
        Asserts.isTrue(CollUtil.isNotEmpty(mapList), "设备编号不存在");
        return mapList.get(0);
    }
}
