package com.steerinfo.baseinfo.meterbasegroupspot.controller;

import com.steerinfo.baseinfo.meterbaserailwaycarriageweight.model.MeterBaseRailwayCarriageWeight;
import com.steerinfo.baseinfo.meterbaseuserspot.model.MeterBaseUserSpot;
import com.steerinfo.framework.controller.BaseRESTfulController;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.framework.user.UserPayload;
import com.steerinfo.framework.utils.collection.ListUtils;
import com.steerinfo.baseinfo.meterbasegroupspot.model.MeterBaseGroupSpot;
import com.steerinfo.baseinfo.meterbasegroupspot.service.IMeterBaseGroupSpotService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.math.BigDecimal;

/**
 * MeterBaseGroupSpot RESTful接口：
 * @author generator
 * @version 1.0-SNAPSHORT 2022-06-29 11:35
 * 类描述
 * 修订历史：
 * 日期：2022-06-29
 * 作者：generator
 * 参考：
 * 描述：MeterBaseGroupSpot RESTful接口
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@RestController
@RequestMapping("/${api.version}/meterbasegroupspots")
public class MeterBaseGroupSpotController extends BaseRESTfulController {

    @Autowired
    IMeterBaseGroupSpotService meterBaseGroupSpotService;

    @ApiOperation(value="获取列表", notes="分页查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("meterbasegroupspot:view")
    @GetMapping(value = "/")
    public RESTfulResult list(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        PageList<MeterBaseGroupSpot> list = meterBaseGroupSpotService.queryForPage(parmas, pageNum, pageSize);
        return success(list);
    }

    @ApiOperation(value="获取列表", notes="分页模糊查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("meterbasegroupspot:view")
    @GetMapping(value = "/like/")
    public RESTfulResult listLike(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        PageList<MeterBaseGroupSpot> list = meterBaseGroupSpotService.queryLikeForPage(parmas, pageNum, pageSize);
        return success(list);
    }
    
    @ApiOperation(value="创建", notes="根据MeterBaseGroupSpot对象创建")
    @ApiImplicitParam(name = "meterBaseGroupSpot", value = "详细实体meterBaseGroupSpot", required = true, dataType = "MeterBaseGroupSpot")
    //@RequiresPermissions("meterbasegroupspot:create")
    @PostMapping(value = "/")
    public RESTfulResult add(@ModelAttribute MeterBaseGroupSpot model){
        HashMap<String,Object> map = new HashMap<>();
        map.put("scale",model.getSpotId());
        map.put("spotId",model.getSpotId());
        map.put("valueFlag","1");
        if (meterBaseGroupSpotService.getByParams(map) != null){
            return failed(null,"已存在对应的数据");
        }

        model.setGroupSpotId(meterBaseGroupSpotService.creatId());
        UserPayload payload = UserPayload.getCurrUser();
        model.setCreateManNo(payload.getUserCode());
        model.setCreateManName(payload.getUserName());
        model.setCreateTime(new Date());
        model.setValueFlag("1");
        MeterBaseGroupSpot meterBaseGroupSpot = meterBaseGroupSpotService.add(model);
        return success(meterBaseGroupSpot);
    }

    @ApiOperation(value="获取详细信息", notes="根据url的id来获取详细信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("meterbasegroupspot:view")
    @GetMapping(value = "/{id}")
    public RESTfulResult get(@PathVariable String id){
        MeterBaseGroupSpot meterBaseGroupSpot = meterBaseGroupSpotService.getById(id);
        return success(meterBaseGroupSpot);
    }

    @ApiOperation(value="更新详细信息", notes="根据url的id来指定更新对象，并根据传过来的meterBaseGroupSpot信息来更新详细信息")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String"),
        @ApiImplicitParam(name = "meterBaseGroupSpot", value = "详细实体meterBaseGroupSpot", required = true, dataType = "MeterBaseGroupSpot")
    })
    //@RequiresPermissions("meterbasegroupspot:update")
    @PutMapping(value = "/{id}", produces  = "application/json;charset=UTF-8")
    public RESTfulResult update(@PathVariable String id, @RequestBody MeterBaseGroupSpot model){
        model.setId(id);
        UserPayload payload = UserPayload.getCurrUser();
        model.setUpdateManNo(payload.getUserCode());
        model.setUpdateManName(payload.getUserName());
        model.setUpdateTime(new Date());
        MeterBaseGroupSpot meterBaseGroupSpot = meterBaseGroupSpotService.modify(model);
        return success(meterBaseGroupSpot);
    }

    @ApiOperation(value="删除", notes="根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("meterbasegroupspot:delete")
    @DeleteMapping(value = "/{id}")//String
    public RESTfulResult delete(@PathVariable String id){
    	List<String> list = Arrays.asList(id.split(","));
    	if(ListUtils.isNotEmpty(list)) {
	    	List<String> ids = ListUtils.convertList(list);
			  meterBaseGroupSpotService.delete(ids);
    	}
      return success();
    }

    @ApiOperation(value="查询", notes="根据group和spot相关参数查询")
    //@RequiresPermissions("meterbaserailwaycarriageweight:delete")
    @GetMapping(value = "/queryByGroupSpot")//String
    public RESTfulResult queryByGroupSpot(@RequestParam HashMap<String,Object> map,Integer pageNum, Integer pageSize){
        PageList<MeterBaseGroupSpot> list = meterBaseGroupSpotService.queryByGroupSpotForPage(map,pageNum, pageSize);
        return success(list);
    }

    @ApiOperation(value="查询", notes="根据group和spot相关参数查询")
    //@RequiresPermissions("meterbaserailwaycarriageweight:delete")
    @GetMapping(value = "/noPage")//String
    public RESTfulResult noPage(@RequestParam HashMap<String,Object> map){
        List<MeterBaseGroupSpot> list = meterBaseGroupSpotService.noPage(map);
        return success(list);
    }

    @ApiOperation(value="根据用户和计量点ID创建对象", notes="根据meter_base_group_spot表和meter_base_spot_info表的id创建MeterBaseUserSpot对象")
    @PostMapping(value = "/checkAdd")
    public RESTfulResult checkAdd(@RequestBody List<MeterBaseGroupSpot> model){
        RESTfulResult rm = failed();
        try{
            rm = meterBaseGroupSpotService.checkAdd(model);
            return success(rm);
        }catch(Exception e){
            e.printStackTrace();
            return rm;
        }
    }

    @ApiOperation(value="根据List<MeterBaseUserSpot>解除对象", notes="根据List<MeterBaseUserSpot>对象解除分组站点信息")
    @PostMapping(value = "/remove")
    public RESTfulResult remove(@RequestBody List<MeterBaseGroupSpot> model){
        RESTfulResult rm = failed();
        try{
            rm = meterBaseGroupSpotService.remove(model);
            return success(rm);
        }catch(Exception e){
            e.printStackTrace();
            return rm;
        }
    }

    @ApiOperation(value="逻辑删除", notes="逻辑删除")
    //@RequiresPermissions("meterbaserailwaycarriageweight:delete")
    @PutMapping(value = "/deleteByLogic")//String
    public RESTfulResult deleteByLogic(@RequestParam String id){
        //判断主键是否存在
        MeterBaseGroupSpot meterBaseGroupSpot = meterBaseGroupSpotService.queryById(id);
        if (meterBaseGroupSpot == null){
            return failed(null,"找不到主键对应的值");
        }
        meterBaseGroupSpot.setValueFlag("0");
        meterBaseGroupSpotService.deleteByLogic(meterBaseGroupSpot);
        return success(null,"删除成功");
    }
}
