package com.steerinfo.baseinfo.combaseinfo.controller;


import com.steerinfo.baseinfo.combaseinfo.model.ComBaseInfo;
import com.steerinfo.baseinfo.combaseinfo.service.IComBaseInfoService;
import com.steerinfo.framework.controller.BaseRESTfulController;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.framework.utils.collection.ListUtils;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.meterwork.meterworkstoragecalibration.model.MeterWorkStorageCalibration;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

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

    @Autowired
    IComBaseInfoService comBaseInfoService;

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

    @ApiOperation(value = "创建", notes = "根据ComBaseInfo对象创建")
    @ApiImplicitParam(name = "comBaseInfo", value = "详细实体comBaseInfo", required = true, dataType = "ComBaseInfo")
    @Transactional
    @PostMapping(value = "/")
    public RESTfulResult add(@RequestBody ComBaseInfo model) {
        try {
            String msg = comBaseInfoService.checkandadd(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("combaseinfo:view")
    @GetMapping(value = "/{id}")
    public RESTfulResult get(@PathVariable String id) {
        try {
            ComBaseInfo comBaseInfo = comBaseInfoService.getById(id);
            return success(comBaseInfo);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

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

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

    @ApiOperation(value = "启用标识符", notes = "根据传过来的ComBaseInfo集合信息来更新详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ComBaseInfo", value = "详细实体ComBaseInfo", required = true, dataType = "ComBaseInfo")
    })
    @Transactional
    @PostMapping(value = "/Recovery")
    public RESTfulResult Recovery(@RequestBody List<ComBaseInfo> models) {
        try {
            String msg = comBaseInfoService.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 = "根据传过来的ComBaseInfo集合信息来更新详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ComBaseInfo", value = "详细实体ComBaseInfo", required = true, dataType = "ComBaseInfo")
    })
    //@RequiresPermissions("combaseinfo:update")
    @Transactional
    @PostMapping(value = "/abolish")
    public RESTfulResult abolish(@RequestBody List<ComBaseInfo> models) {
        try {
            String msg = comBaseInfoService.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(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    @GetMapping(value = "/selectSpoType")
    public RESTfulResult selectSpoType() {
        List<ComBaseInfo> list = comBaseInfoService.selectSpoType();
        return success(list);
    }

    @ApiOperation(value = "获取硬盘录像机类型", notes = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    @GetMapping(value = "/selectVideoType")
    public RESTfulResult selectVideoType() {
        try {
            List<ComBaseInfo> list = comBaseInfoService.selectVideoType();
            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")
    })
    @GetMapping(value = "/selectVideoMark")
    public RESTfulResult selectVideoMark(String baseName, String baseCode) {
        RESTfulResult rm = failed();
        try {
            List<ComBaseInfo> list = comBaseInfoService.selectVideoMark(baseName, baseCode);
            return success(list);
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        } finally {

        }
        return rm;
    }

    @ApiOperation(value = "获取列表", notes = "查询所有不分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    @GetMapping(value = "/noPage")
    public RESTfulResult list(@RequestParam HashMap parmas) {
        try {
            List<ComBaseInfo> list = comBaseInfoService.queryAll(parmas);
            Collections.reverse(list);
            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")
    })
    @GetMapping(value = "/noPageNoLike")
    public RESTfulResult listNoLike(@RequestParam HashMap parmas) {
        try {
            List<ComBaseInfo> list = comBaseInfoService.query(parmas);
            Collections.reverse(list);
            return success(list);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "执行Sql获取列表")
    @PostMapping(value = "/executeSqlDataWf", produces = "application/json;charset=UTF-8")
    public RESTfulResult executeSqlDataWf(@RequestBody String sql) {
        try {
            RESTfulResult rm = comBaseInfoService.executeSqlDataWf(sql);
            return rm;
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "修改数据,客户端用", notes = "界面填写数据，服务端做二次校验及赋值")
    @Transactional(rollbackFor = {MarkerMetException.class, Exception.class})
    @PostMapping(value = "/doUpdateWf")
    public RESTfulResult doUpdateWf(@RequestBody ComBaseInfo model) {
        RESTfulResult rm = failed();
        try {
            String str = comBaseInfoService.doUpdateWf(model);
            if (StringUtils.isEmpty(str)) {
                return success();
            } else {
                return failed("操作失败!" + str);
            }
        }catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
            rm.setData(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！"+e.getMessage());
            rm.setData("操作异常，请确认！");
        } finally {

        }
        return rm;
    }

    @ApiOperation(value = "计量员班长配置获取有效信息列表", notes="关键字:pBaseCode:001005、validFlag：1")
    @PostMapping(value = "/measurementLeaderNo", produces = "application/json;charset=UTF-8")
    public RESTfulResult measurementLeaderNo(@RequestBody(required = false) HashMap<String, Object> params) {
        List<ComBaseInfo> models = comBaseInfoService.measurementLeaderNo(params);
        return success(models);
    }

    @ApiOperation(value = "获取用户审核权限等级", notes="关键字:1：计量数据管理员、2：计量班长、3：计量员、4：没有权限")
    @PostMapping(value = "/checkManLevel", produces = "application/json;charset=UTF-8")
    public RESTfulResult checkManLevel(@RequestBody(required = false) HashMap<String, Object> params) {
        String str = comBaseInfoService.checkManLevel(params);
        return success(str);
    }

}
