package com.steerinfo.meterwork.meterworkmeasuringinstrumentsecond.controller;

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.meterworkmeasuringinstrumentsecond.model.batchAddInfoBo;
import com.steerinfo.meterwork.meterworkmeasuringinstrumentsecond.model.MeterWorkMeasuringInstrumentSecond;
import com.steerinfo.meterwork.meterworkmeasuringinstrumentsecond.service.IMeterWorkMeasuringInstrumentSecondService;
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.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * MeterWorkMeasuringInstrumentSecond RESTful接口：
 * @author generator
 * @version 1.0-SNAPSHORT 2023-02-24 09:12
 * 类描述
 * 修订历史：
 * 日期：2023-02-24
 * 作者：generator
 * 参考：
 * 描述：MeterWorkMeasuringInstrumentSecond RESTful接口
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@RestController
@RequestMapping("/${api.version}/meterworkmeasuringinstrumentseconds")
public class MeterWorkMeasuringInstrumentSecondController extends BaseRESTfulController {

    @Autowired
    IMeterWorkMeasuringInstrumentSecondService meterWorkMeasuringInstrumentSecondService;

    @ApiOperation(value="获取列表", notes="分页查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("meterworkmeasuringinstrumentsecond:view")
    @GetMapping(value = "/")
    public RESTfulResult list(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        PageList<MeterWorkMeasuringInstrumentSecond> list = meterWorkMeasuringInstrumentSecondService.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("meterworkmeasuringinstrumentsecond:view")
    @GetMapping(value = "/like/")
    public RESTfulResult listLike(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        PageList<MeterWorkMeasuringInstrumentSecond> list = meterWorkMeasuringInstrumentSecondService.queryLikeForPage(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("meterworkmeasuringinstrumentsecond:view")
    @GetMapping(value = "/likeDesc/")
    public RESTfulResult likeDesc(@RequestParam HashMap parmas){
        Integer pageNum = parmas.get("pageNum") == null ? 1 : Integer.parseInt(parmas.get("pageNum").toString());
        Integer pageSize =parmas.get("pageSize") == null ? 20 : Integer.parseInt(parmas.get("pageSize").toString());
        PageList<MeterWorkMeasuringInstrumentSecond> list = meterWorkMeasuringInstrumentSecondService.likeDesc(parmas, pageNum, pageSize);
        return success(list);
    }

    @ApiOperation(value="获取录入人列表", notes="不分页查询（createManName）")
    @GetMapping(value = "/getCreateManNo")
    public RESTfulResult getCreateManNo(@RequestParam(required = false) HashMap parmas){
        List<String> list = meterWorkMeasuringInstrumentSecondService.getCreateManNo(parmas);
        return success(list);
    }
    
    @ApiOperation(value="创建", notes="根据MeterWorkMeasuringInstrumentSecond对象创建")
    @ApiImplicitParam(name = "meterWorkMeasuringInstrumentSecond", value = "详细实体meterWorkMeasuringInstrumentSecond", required = true, dataType = "MeterWorkMeasuringInstrumentSecond")
    //@RequiresPermissions("meterworkmeasuringinstrumentsecond:create")
    @PostMapping(value = "/")
    public RESTfulResult add(@RequestBody MeterWorkMeasuringInstrumentSecond model){
        try{
            String msg = meterWorkMeasuringInstrumentSecondService.checkandadd(model);
            if (msg != "" && !msg.equals("")) {
                return  failed(null,msg);
            }
        }catch (Exception e){
            e.printStackTrace();
            return failed(null,"操作异常，请确认！");
        }
        return success();
    }

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

    @ApiOperation(value="更新详细信息", notes="根据url的id来指定更新对象，并根据传过来的meterWorkMeasuringInstrumentSecond信息来更新详细信息")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String"),
        @ApiImplicitParam(name = "meterWorkMeasuringInstrumentSecond", value = "详细实体meterWorkMeasuringInstrumentSecond", required = true, dataType = "MeterWorkMeasuringInstrumentSecond")
    })
    //@RequiresPermissions("meterworkmeasuringinstrumentsecond:update")
    @PutMapping(value = "/{id}", produces  = "application/json;charset=UTF-8")
    public RESTfulResult update(@PathVariable String id, @RequestBody MeterWorkMeasuringInstrumentSecond model){
        model.setId(id);
        MeterWorkMeasuringInstrumentSecond meterWorkMeasuringInstrumentSecond = meterWorkMeasuringInstrumentSecondService.modify(model);
        return success(meterWorkMeasuringInstrumentSecond);
    }

    @ApiOperation(value="删除", notes="根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("meterworkmeasuringinstrumentsecond: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);
			  meterWorkMeasuringInstrumentSecondService.delete(ids);
    	}
      return success();
    }

    @ApiOperation(value = "批量导入", notes = "根据MeterWorkMeasuringInstrument对象批量导入")
    @ApiImplicitParam(paramType = "path",  required = true, dataType = "List<MeterWorkMeasuringInstrument>")
    @Transactional(rollbackFor = MarkerMetException.class)
    @PostMapping(value = "/batchAddInfo")//String
    public RESTfulResult batchAddInfo(@RequestBody batchAddInfoBo map) {
        try {

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date grossTime = sdf.parse(map.getGrossTime());
            Date tareTime = sdf.parse(map.getTareTime());
            List<MeterWorkMeasuringInstrumentSecond> models =  map.getList();
            models.get(0).setGrossTime(grossTime);
            models.get(0).setTareTime(tareTime);
//            for(int i=0; i < mapList.size(); i++) {
//                MeterWorkMeasuringInstrument model = new MeterWorkMeasuringInstrument();
//                if(i == 0) {
//                    model.setGrossTime(grossTime);
//                    model.setTareTime(tareTime);
//                }
//                String railwayNo = mapList.get(i).get("railwayNo").toString();
//                BigDecimal grossWeight = new BigDecimal(mapList.get(i).get("grossWeight").toString());
//                BigDecimal tareWeight = new BigDecimal(mapList.get(i).get("tareWeight").toString());
//                model.setRailwayNo(railwayNo);
//                model.setGrossWeight(grossWeight);
//                model.setTareWeight(tareWeight);
//                model.setNetWeight(new BigDecimal(mapList.get(i).get("netWeight").toString()));
//                model.setMatterName(mapList.get(i).get("matterName").toString());
//                model.setForwardingUnitName(mapList.get(i).get("forwardingUnitName").toString());
//                model.setReceivingUintName(mapList.get(i).get("receivingUintName").toString());
//                model.setCarrierUnitName(mapList.get(i).get("carrierUnitName").toString());
//                models.add(model);
//            }
            RESTfulResult rm = meterWorkMeasuringInstrumentSecondService.batchAddInfo(models);
            return rm;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "批量作废", notes = "根据MeterWorkMeasuringInstrument对象批量作废")
    @ApiImplicitParam(paramType = "path",  required = true, dataType = "List<MeterWorkMeasuringInstrument>")
    @Transactional
    @PostMapping(value = "/batchDeleteInfo")//String
    public RESTfulResult batchDeleteInfo(@RequestBody List<MeterWorkMeasuringInstrumentSecond> models) {
        try {
            RESTfulResult rm = meterWorkMeasuringInstrumentSecondService.batchDeleteInfo(models);
            return rm;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }
}
