package com.mx.cneeds.server.device.web;

import com.mx.cneeds.common.dto.DeviceLinkProtocolsDto;
import com.mx.cneeds.common.pager.PageUtils;
import com.mx.cneeds.common.result.R;
import com.mx.cneeds.common.validator.ValidatorUtils;
import com.mx.cneeds.server.device.service.DeviceLinkProtocolsService;
import com.mx.cneeds.server.entity.DeviceLinkProtocolsEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @ClassName DeviceLinkProtocolsController
 * @Author zzrdark
 * @Date 2020-08-14 16:40
 * @Description TODO
 **/
@RestController
@RequestMapping("device/devicelinkprotocols")
public class DeviceLinkProtocolsController {

    @Autowired
    private DeviceLinkProtocolsService deviceLinkProtocolsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params){
        PageUtils page = deviceLinkProtocolsService.queryPage(params);
        List<DeviceLinkProtocolsDto> list = new ArrayList<>();
        for (Object entity : page.getList()){
            DeviceLinkProtocolsDto dto = new DeviceLinkProtocolsDto();
            DeviceLinkProtocolsEntity deviceLinkProtocolsEntity = (DeviceLinkProtocolsEntity) entity;
            BeanUtils.copyProperties(deviceLinkProtocolsEntity,dto);
            list.add(dto);
        }
        page.setList(list);
        return page;
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody DeviceLinkProtocolsDto deviceCarTypeDto){
        DeviceLinkProtocolsEntity deviceLinkProtocolsEntity = new DeviceLinkProtocolsEntity();
        BeanUtils.copyProperties(deviceCarTypeDto,deviceLinkProtocolsEntity);
        deviceLinkProtocolsEntity.setUpdateTime(new Date());
        deviceLinkProtocolsService.save(deviceLinkProtocolsEntity);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody DeviceLinkProtocolsDto deviceLinkProtocolsDto){
        ValidatorUtils.validateEntity(deviceLinkProtocolsDto);
        DeviceLinkProtocolsEntity deviceLinkProtocolsEntity = new DeviceLinkProtocolsEntity();
        BeanUtils.copyProperties(deviceLinkProtocolsDto,deviceLinkProtocolsEntity);
        deviceLinkProtocolsEntity.setUpdateTime(new Date());
        deviceLinkProtocolsService.updateById(deviceLinkProtocolsEntity);
        return R.ok();
    }

    /**
     * 删除渠道号
     */
    @RequestMapping("/delete")
    @Transactional
    public R delete(@RequestBody List<Long> linkProtocolsIds){
        deviceLinkProtocolsService.removeByIds(linkProtocolsIds);
        return R.ok();
    }

}
