package com.steerinfo.baseinfo.meterbasecalibrarelation.controller;

import com.steerinfo.baseinfo.meterbasecalibrationtime.model.MeterBaseCalibrationTime;
import com.steerinfo.baseinfo.meterbasespotinfo.model.MeterBaseSpotInfo;
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.baseinfo.meterbasecalibrarelation.model.MeterBaseCalibraRelation;
import com.steerinfo.baseinfo.meterbasecalibrarelation.service.IMeterBaseCalibraRelationService;
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.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.HashMap;
import java.util.List;
import java.util.ArrayList;

/**
 * MeterBaseCalibraRelation RESTful接口：
 *
 * @author generator
 * @version 1.0-SNAPSHORT 2020-06-16 03:08
 * 类描述
 * 修订历史：
 * 日期：2020-06-16
 * 作者：generator
 * 参考：
 * 描述：MeterBaseCalibraRelation RESTful接口
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 * @see null
 */
@RestController
@RequestMapping("/${api.version}/meterbasecalibrarelations")
@Api(value = "/${api.version}/meterbasecalibrarelations", tags = "校秤时间关联配置 - MeterBaseCalibraRelationController")
public class MeterBaseCalibraRelationController extends BaseRESTfulController {

    @Autowired
    IMeterBaseCalibraRelationService meterBaseCalibraRelationService;

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

    @ApiOperation(value = "创建", notes = "根据MeterBaseCalibraRelation对象创建")
    @ApiImplicitParam(name = "meterBaseCalibraRelation", value = "详细实体meterBaseCalibraRelation", required = true, dataType = "MeterBaseCalibraRelation")
    //@RequiresPermissions("meterbasecalibrarelation:create")
    @Transactional
    @PostMapping(value = "/")
    public RESTfulResult add(@RequestBody List<MeterBaseCalibraRelation> models) {
        try {
            String msg = meterBaseCalibraRelationService.checkandadd(models);
            if (StringUtils.isNotEmpty(msg)) {
                return failed(null, msg);
            } else {
                return success();
            }
        } 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("meterbasecalibrarelation:view")
    @GetMapping(value = "/{id}")
    public RESTfulResult get(@PathVariable String id) {
        try {
            MeterBaseCalibraRelation meterBaseCalibraRelation = meterBaseCalibraRelationService.getById(id);
            return success(meterBaseCalibraRelation);
        } catch (Exception ex) {
           ex.printStackTrace();
throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "更新详细信息", notes = "根据url的id来指定更新对象，并根据传过来的meterBaseCalibraRelation信息来更新详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "meterBaseCalibraRelation", value = "详细实体meterBaseCalibraRelation", required = true, dataType = "MeterBaseCalibraRelation")
    })
    //@RequiresPermissions("meterbasecalibrarelation:update")
    @PutMapping(value = "/{id}", produces = "application/json;charset=UTF-8")
    public RESTfulResult update(@PathVariable String id, @RequestBody MeterBaseCalibraRelation model) {
        try {
            model.setId(id);
            MeterBaseCalibraRelation meterBaseCalibraRelation = meterBaseCalibraRelationService.modify(model);
            return success(meterBaseCalibraRelation);
        } catch (Exception ex) {
           ex.printStackTrace();
throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "删除", notes = "根据url的实体集来指定删除对象")
    @ApiImplicitParam(name = "MeterBaseCalibraRelation", value = "详细实体MeterBaseCalibraRelation", required = true, dataType = "MeterBaseCalibraRelation")
    @PutMapping(value = "/")
    public RESTfulResult delete(@RequestBody List<MeterBaseCalibraRelation> models) {
        try {
            List<String> list = new ArrayList<String>();
            for (MeterBaseCalibraRelation model : models) {
                String id = model.getId();
                list.add(id);
            }
            List<String> ids = ListUtils.convertList(list);
            meterBaseCalibraRelationService.delete(ids);
            return success();
        } catch (Exception ex) {
           ex.printStackTrace();
throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "新增计量点校称时间", notes = "根据MeterBaseCalibrationTime,MeterBaseSpotInfo对象创建")
    //@ApiImplicitParams({
    //        @ApiImplicitParam(name = "meterBaseSpotInfo", value = "详细实体meterBaseSpotInfo", required = true, dataType = "MeterBaseSpotInfo"),
    //        @ApiImplicitParam(name = "meterBaseCalibrationTime", value = "详细实体meterBaseCalibrationTime", required = true, dataType = "MeterBaseCalibrationTime")
    //})
    @Transactional
    @PostMapping(value = "/addSpotTime1")
    public RESTfulResult addSpotTime1(@RequestBody MeterBaseCalibrationTime mbCalibrationTime, @RequestBody MeterBaseSpotInfo mbSpotInfo) {
        try {
            String msg = meterBaseCalibraRelationService.addSpotTime(mbCalibrationTime,mbSpotInfo);
            if (msg != null && !msg.equals("")) {
                return failed(null, msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "操作异常，请确认！");
        }
        return success();
    }

    @ApiOperation(value = "新增计量点校称时间", notes = "根据MeterBaseCalibrationTime,MeterBaseSpotInfo对象创建")
    @Transactional
    @PostMapping(value = "/addSpotTime")
    public RESTfulResult addSpotTime(@RequestBody MeterBaseCalibrationTime mbCalibrationTime, @RequestParam(required = true) String baseSpotNo,@RequestParam(required = true) String baseSpotName) {
        try {
            MeterBaseSpotInfo mbSpotInfo = new MeterBaseSpotInfo();
            mbSpotInfo.setBaseSpotNo(baseSpotNo);
            mbSpotInfo.setBaseSpotName(baseSpotName);
            String msg = meterBaseCalibraRelationService.addSpotTime(mbCalibrationTime,mbSpotInfo);
            if (msg != null && !msg.equals("")) {
                return failed(null, msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "操作异常，请确认！"+e.getMessage());
        }
        return success();
    }


    @ApiOperation(value = "废除标识符", notes = "根据传过来的MeterBaseCalibraRelation集合信息来更新详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "MeterBaseCalibraRelation", value = "详细实体MeterBaseCalibraRelation", required = true, dataType = "MeterBaseCalibraRelation")
    })
    //@RequiresPermissions("combaseinfo:update")
    @Transactional
    @PostMapping(value = "/abolish")
    public RESTfulResult abolish(@RequestBody List<MeterBaseCalibraRelation> models) {
        try {
            String msg = meterBaseCalibraRelationService.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 = "根据传过来的MeterBaseCalibrationTime集合信息来更新详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "MeterBaseCalibrationTime", value = "详细实体MeterBaseCalibrationTime", required = true, dataType = "MeterBaseCalibrationTime")
    })
    @Transactional
    @PostMapping(value = "/updateBymbCalibTimeNo")
    public RESTfulResult updateBymbCalibTimeNo(@RequestParam(required = true) String calTimeNo,@RequestBody MeterBaseCalibrationTime models) {
        try {
            String msg = meterBaseCalibraRelationService.updateBymbCalibTimeNo(calTimeNo,models);
            if (StringUtils.isNotEmpty(msg)) {
                return failed(null, msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "操作异常，请确认！");
        }
        return success();
    }


}
