package com.zcxk.rmcp.web.service.impl;

import com.github.pagehelper.PageHelper;
import com.zcxk.core.common.enums.StatusEnum;
import com.zcxk.core.oauth2.util.UserUtil;
import com.zcxk.core.mysql.pageing.Pagination;
import com.zcxk.rmcp.api.dto.devicecommand.DeviceCommandPageDto;
import com.zcxk.rmcp.api.enums.CommandStatusEnum;
import com.zcxk.rmcp.api.enums.CommandTypeEnum;
import com.zcxk.rmcp.api.enums.DeviceStatusEnum;
import com.zcxk.rmcp.api.enums.OperationTypeEnum;
import com.zcxk.rmcp.api.vo.DeviceCommandVo;
import com.zcxk.rmcp.web.service.DeviceCommandService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.zcxk.rmcp.core.dao.DeviceCommandMapper;
import com.zcxk.rmcp.core.entity.DeviceCommand;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 
 * @author linqingwei
 * @date 2021-07-20 16:16
 */
@Service
public class DeviceCommandServiceImpl implements DeviceCommandService {

    @Resource
    private DeviceCommandMapper deviceCommandMapper;

    @Override
    public int insertSelective(DeviceCommand record) {
        return deviceCommandMapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(DeviceCommand record) {
        return deviceCommandMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public DeviceCommandVo findDeviceCommand(Long id){
        return deviceCommandMapper.findDeviceCommand(id);
    }

    @Override
    public Pagination<DeviceCommandVo> pageDeviceCommand(DeviceCommandPageDto pageDto) {
        PageHelper.startPage(pageDto.getPageNum(), pageDto.getPageSize());
        List<DeviceCommandVo> list = listDeviceCommand(pageDto);
        if (CollectionUtils.isEmpty(list)){
            return new Pagination<>();
        }
        return new Pagination<>(list);
    }

    @Override
    public List<DeviceCommandVo> listDeviceCommand(DeviceCommandPageDto pageDto) {
        List<DeviceCommandVo> list =  deviceCommandMapper.listDeviceCommand(pageDto, UserUtil.getCurrentUser().getUserCondition());
        if (CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        for (DeviceCommandVo deviceCommandVo : list) {
            deviceCommandVo.setCommandStatus(CommandStatusEnum.getMessage(deviceCommandVo.getCommandStatus()));
            deviceCommandVo.setCommandType(CommandTypeEnum.getMessage(deviceCommandVo.getCommandType()));
            deviceCommandVo.setOperationType(OperationTypeEnum.getMessage(Integer.parseInt(deviceCommandVo.getOperationType())));
            deviceCommandVo.setDeviceStatus(DeviceStatusEnum.getMessage(deviceCommandVo.getDeviceStatus()));
        }
        return list;
    }

    @Override
    public void delete(Long id) {
        deviceCommandMapper.updateStatus(Arrays.asList(id), StatusEnum.DELETE.getCode());
    }

    @Override
    public void deleteAll(List<Long> ids) {
        deviceCommandMapper.updateStatus(ids, StatusEnum.DELETE.getCode());
    }

}
