package com.steerinfo.baseinfo.meterbasespotinfo.controller;

import com.steerinfo.framework.controller.BaseRESTfulController;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.service.pagehelper.PageHelper;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.framework.utils.collection.ListUtils;
import com.steerinfo.baseinfo.meterbasespotinfo.mapper.MeterBaseSpotInfoMapper;
import com.steerinfo.baseinfo.meterbasespotinfo.model.MeterBaseSpotInfo;
import com.steerinfo.baseinfo.meterbasespotinfo.service.IMeterBaseSpotInfoService;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.pl.REGON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * MeterBaseSpotInfo RESTful接口：
 *
 * @author generator
 * @version 1.0-SNAPSHORT 2020-06-05 06:15
 * 类描述
 * 修订历史：
 * 日期：2020-06-05
 * 作者：generator
 * 参考：
 * 描述：MeterBaseSpotInfo RESTful接口
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 * @see null
 */
@RestController
@RequestMapping("/${api.version}/meterbasespotinfos")
@Api(value = "/${api.version}/meterbasespotinfos", tags = "计量点配置 - MeterBaseSpotInfoController")
public class MeterBaseSpotInfoController extends BaseRESTfulController {

    @Autowired
    IMeterBaseSpotInfoService meterBaseSpotInfoService;

    @Resource
    MeterBaseSpotInfoMapper meterBaseSpotInfoMapper;


    @ApiOperation(value = "获取列表", notes = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("meterbasespotinfo:view")
    @GetMapping(value = "/")
    public RESTfulResult list(@RequestParam HashMap parmas, Integer pageNum, Integer pageSize) {
        try {
            PageList<MeterBaseSpotInfo> list = meterBaseSpotInfoService.queryForPage(parmas, pageNum, pageSize);
            return success(list);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "获取静态铁水衡、吊钩秤站点列表", notes = "不分页查询")
    @GetMapping(value = "/TSDGSpotInfo")
    public RESTfulResult list(@RequestParam HashMap parmas) {
        try {
            List<MeterBaseSpotInfo> list = meterBaseSpotInfoService.TSDGSpotInfo(parmas);
            return success(list);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }


    @ApiOperation(value = "获取列表", notes = "不分页查询")
    @GetMapping(value = "/noPage")
    public RESTfulResult listall(@RequestParam HashMap parmas) {
        try {
            List<MeterBaseSpotInfo> list = meterBaseSpotInfoService.queryall(parmas);
            return success(list);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "动态衡获取列表", notes = "不分页查询")
    @GetMapping(value = "/DynamicNoPage")
    public RESTfulResult DynamicNoPage(@RequestParam HashMap parmas) {
        try {
            List<MeterBaseSpotInfo> list = meterBaseSpotInfoService.DynamicNoPage(parmas);
            return success(list);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }


    @ApiOperation(value = "获取列表", notes = "分页模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("meterbasespotinfo:view")
    @GetMapping(value = "/like/")
    public RESTfulResult listLike(@RequestParam HashMap parmas, Integer pageNum, Integer pageSize) {
        try {
            PageList<MeterBaseSpotInfo> list = meterBaseSpotInfoService.queryLikeForPage(parmas, pageNum, pageSize);
            return success(list);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "创建", notes = "根据MeterBaseSpotInfo对象创建")
    @ApiImplicitParam(name = "meterBaseSpotInfo", value = "详细实体meterBaseSpotInfo", required = true, dataType = "MeterBaseSpotInfo")
    //@RequiresPermissions("meterbasespotinfo:create")
    @Transactional
    @PostMapping(value = "/")
    public RESTfulResult add(@RequestBody MeterBaseSpotInfo model) {
        try {
            RESTfulResult rm = meterBaseSpotInfoService.checkandadd(model);
            return rm;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "操作异常，请确认！");
        }
    }

    @ApiOperation(value = "获取详细信息", notes = "根据url的id来获取详细信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("meterbasespotinfo:view")
    @GetMapping(value = "/{id}")
    public RESTfulResult get(@PathVariable String id) {
        try {
            MeterBaseSpotInfo meterBaseSpotInfo = meterBaseSpotInfoService.getById(id);
            return success(meterBaseSpotInfo);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "更新详细信息", notes = "根据url的id来指定更新对象，并根据传过来的meterBaseSpotInfo信息来更新详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "meterBaseSpotInfo", value = "详细实体meterBaseSpotInfo", required = true, dataType = "MeterBaseSpotInfo")
    })
    //@RequiresPermissions("meterbasespotinfo:update")
    @Transactional
    @PutMapping(value = "/{id}", produces = "application/json;charset=UTF-8")
    public RESTfulResult update(@PathVariable String id, @RequestBody MeterBaseSpotInfo model) {
        try {
            String msg = meterBaseSpotInfoService.checkandupdate(id, model);
            if (StringUtils.isNotEmpty(msg)) {
                return failed(null, msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "操作异常，请确认！");
        }

        return success();
    }

    @ApiOperation(value = "删除", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("meterbasespotinfo:delete")
    @DeleteMapping(value = "/{id}")//String
    public RESTfulResult delete(@PathVariable String id) {
        try {
            List<String> list = Arrays.asList(id.split(","));
            if (ListUtils.isNotEmpty(list)) {
                List<String> ids = ListUtils.convertList(list);
                meterBaseSpotInfoService.delete(ids);
            }
            return success();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "启用标识符", notes = "根据传过来的MeterBaseSpotInfo集合信息来更新详细信息")
    @Transactional
    @PostMapping(value = "/Recovery")
    public RESTfulResult recovery(@RequestBody List<MeterBaseSpotInfo> models) {
        try {
            String msg = meterBaseSpotInfoService.recovery(models);
            if (StringUtils.isNotEmpty(msg)) {
                return failed(null, msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "操作异常，请确认！");
        }
        return success();
    }

    @ApiOperation(value = "废除标识符", notes = "根据传过来的MeterBaseSpotInfo集合信息来更新详细信息")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "MeterBaseSpotInfo", value = "详细实体MeterBaseSpotInfo", required = true, dataType = "MeterBaseSpotInfo")
    })
    //@RequiresPermissions("combaseinfo:update")
    @Transactional
    @PostMapping(value = "/abolish")
    public RESTfulResult abolish(@RequestBody List<MeterBaseSpotInfo> models) {
        try {
            String msg = meterBaseSpotInfoService.abolish(models);
            if (StringUtils.isNotEmpty(msg)) {
                return failed(null, msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "操作异常，请确认！");
        }
        return success();
    }

    @ApiOperation(value = "获取计量点名称", notes = "获取所有计量点的名称")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String"),
    })
    @GetMapping(value = "/gain")
    public RESTfulResult gain() {
        try {
            List<MeterBaseSpotInfo> name = meterBaseSpotInfoService.selectall();
            return success(name);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "关联查询", notes = "根据url的id来获取详细信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("meterbasematterinfo:view")
    @GetMapping(value = "/correlation")
    public RESTfulResult correlation(@RequestParam Integer pageNum, Integer pageSize) throws Exception {
        try {
            List<MeterBaseSpotInfo> list = meterBaseSpotInfoService.selectcorrelation();
            PageHelper.startPage(pageNum, pageSize);
            PageList pageInfo = new PageList(list);
            return success(pageInfo);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }


    @ApiOperation(value = "校秤计量关联查询-校秤网页", notes = "根据url的id来获取详细信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("meterbasematterinfo:view")
    @GetMapping(value = "/calibcorrelation")
    public RESTfulResult calibcorrelation(@RequestParam Integer pageNum, Integer pageSize) throws Exception {
        try {
            List<MeterBaseSpotInfo> list = meterBaseSpotInfoService.calibcorrelation();
            PageHelper.startPage(pageNum, pageSize);
            PageList pageInfo = new PageList(list);
            return success(pageInfo);
        } catch (
                Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }

    }

    @ApiOperation(value = "Wf更新计量点信息", notes = "根据url的id来指定更新对象，并根据传过来的meterBaseSpotInfo信息来更新详细信息")
    @Transactional
    @PostMapping(value = "/doUpdateWf", produces = "application/json;charset=UTF-8")
    public RESTfulResult doUpdateWf(@RequestBody MeterBaseSpotInfo model) {
        try {
            MeterBaseSpotInfo rows = meterBaseSpotInfoService.doUpdateWf(model);
            List<MeterBaseSpotInfo> list = new ArrayList<>();
            list.add(rows);
            PageHelper.startPage(1, 1);
            PageList pageInfo = new PageList(list);
            return success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "操作异常，请确认！");
        }
    }

    @ApiOperation(value = "Wf更新计量点已用纸张信息", notes = "根据url的id来指定更新对象，并根据传过来的meterBaseSpotInfo信息来更新详细信息")
    @Transactional
    @PostMapping(value = "/doUpdateNumWf", produces = "application/json;charset=UTF-8")
    public RESTfulResult doUpdateNumWf(@RequestBody MeterBaseSpotInfo model) {
        try {
            MeterBaseSpotInfo rows = meterBaseSpotInfoService.doUpdateNumWf(model);
            List<MeterBaseSpotInfo> list = new ArrayList<>();
            list.add(rows);
            PageHelper.startPage(1, 1);
            PageList pageInfo = new PageList(list);
            return success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "操作异常，请确认！");
        }

    }

    @ApiOperation(value = "通过userID获取列表", notes = "不分页查询")
    @GetMapping(value = "/selectSpotByUserID")
    public RESTfulResult selectSpotByUserID(@RequestParam String userID) {
        try {
            HashMap map = new HashMap();
            map.put("userID",userID);
            List<MeterBaseSpotInfo> list = meterBaseSpotInfoMapper.selectSpotByUserID(map);
            return success(list);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "通过userID获取列表", notes = "不分页查询")
    @GetMapping(value = "/selectSpotByConfigUserID")
    public RESTfulResult selectSpotByConfigUserID(@RequestParam String userID) {
        try {
            HashMap map = new HashMap();
            map.put("userID",userID);
            List<MeterBaseSpotInfo> list = meterBaseSpotInfoMapper.selectSpotByConfigUserID(map);
            return success(list);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }
}
