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

import com.mx.cneeds.common.dto.DeviceCarTypeDto;
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.DeviceCarTypeService;
import com.mx.cneeds.server.entity.DeviceCarTypeEntity;
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 DeviceCarTypeController
 * @Author zzrdark
 * @Date 2020-08-14 16:38
 * @Description TODO
 **/
@RestController
@RequestMapping("device/devicecartype")
public class DeviceCarTypeController {


    @Autowired
    private DeviceCarTypeService deviceCarTypeService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params){
        PageUtils page = deviceCarTypeService.queryPage(params);
        List<DeviceCarTypeDto> list = new ArrayList<DeviceCarTypeDto>();
        for (Object entity : page.getList()){
            DeviceCarTypeDto dto = new DeviceCarTypeDto();
            DeviceCarTypeEntity deviceCarTypeEntity = (DeviceCarTypeEntity) entity;
            BeanUtils.copyProperties(deviceCarTypeEntity,dto);
            list.add(dto);
        }
        page.setList(list);
        return page;
    }

    @RequestMapping("/queryDeviceCarTypeAll")
    public List<DeviceCarTypeDto> queryDeviceCarTypeAll(){
        List<DeviceCarTypeEntity> deviceChannelEntities = deviceCarTypeService.list();
        List<DeviceCarTypeDto> deviceCarTypeDtos = new ArrayList<>();
        deviceChannelEntities.forEach(e -> {
            DeviceCarTypeDto dto = new DeviceCarTypeDto();
            BeanUtils.copyProperties(e,dto);
            deviceCarTypeDtos.add(dto);
        });

        return deviceCarTypeDtos;
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody DeviceCarTypeDto deviceCarTypeDto){
        DeviceCarTypeEntity deviceCarTypeEntity = new DeviceCarTypeEntity();
        BeanUtils.copyProperties(deviceCarTypeDto,deviceCarTypeEntity);
        deviceCarTypeEntity.setUpdateTime(new Date());
        deviceCarTypeService.save(deviceCarTypeEntity);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody DeviceCarTypeDto deviceCarTypeDto){
        ValidatorUtils.validateEntity(deviceCarTypeDto);
        DeviceCarTypeEntity deviceCarTypeEntity = new DeviceCarTypeEntity();
        BeanUtils.copyProperties(deviceCarTypeDto,deviceCarTypeEntity);
        deviceCarTypeEntity.setUpdateTime(new Date());
        deviceCarTypeService.updateById(deviceCarTypeEntity);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @Transactional
    public R delete(@RequestBody List<Long> carTypeIds){
        deviceCarTypeService.removeByIds(carTypeIds);
        return R.ok();
    }
}
