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

import java.util.*;

import com.mx.cneeds.common.dto.SeriesDto;
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.ProductSeriesService;
import com.mx.cneeds.server.entity.ProductSeriesEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
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;




/**
 * 
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-03-18 14:44:30
 */
@RestController
@RequestMapping("device/series")
public class ProductSeriesController {

    @Autowired
    private ProductSeriesService productSeriesService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params){
        PageUtils page = productSeriesService.queryPage(params);
        List<SeriesDto> list = new ArrayList<SeriesDto>();
        for (Object entity : page.getList()){
            SeriesDto dto = new SeriesDto();
            ProductSeriesEntity seriesEntity = (ProductSeriesEntity) entity;
            BeanUtils.copyProperties(seriesEntity,dto);
            list.add(dto);
        }
        return page;
    }
    @RequestMapping("listAll")
    public List<SeriesDto> listAll(Map<String, Object> params){
        List<ProductSeriesEntity> productSeriesEntities = productSeriesService.queryList(params);

        List<SeriesDto> list = new ArrayList<SeriesDto>();
        for (Object entity : productSeriesEntities){
            SeriesDto dto = new SeriesDto();
            ProductSeriesEntity seriesEntity = (ProductSeriesEntity) entity;
            BeanUtils.copyProperties(seriesEntity,dto);
            list.add(dto);
        }

        return list;
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{seriesId}")
    public R info(@PathVariable("seriesId") Long seriesId){
        ProductSeriesEntity productSeries = productSeriesService.getById(seriesId);

        return R.ok().put("productSeries", productSeries);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SeriesDto dto){
        ProductSeriesEntity seriesEntity = new ProductSeriesEntity();
        BeanUtils.copyProperties(dto,seriesEntity);
        seriesEntity.setCreateTime(new Date());
        productSeriesService.save(seriesEntity);

        return R.ok();
    }


    @RequestMapping("/querySeriesDto")
    public SeriesDto querySeriesDto(@RequestBody String seriesNum){

        ProductSeriesEntity seriesEntity = productSeriesService.queryBySeriesNum(seriesNum);

        if (seriesEntity==null){
            return null;
        }
        SeriesDto dto = new SeriesDto();
        BeanUtils.copyProperties(seriesEntity,dto);

        return dto;
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SeriesDto dto){
        ProductSeriesEntity seriesEntity = new ProductSeriesEntity();
        BeanUtils.copyProperties(dto,seriesEntity);
        ValidatorUtils.validateEntity(seriesEntity);
        productSeriesService.updateById(seriesEntity);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] seriesIds){
        productSeriesService.removeByIds(Arrays.asList(seriesIds));
        return R.ok();
    }

}
