package com.steerinfo.meterwork.meterworkstorageactual.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.dto.StaffPerformanceAnalysis;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.meterwork.meterworkstorageactual.model.MeterWorkStorageActual;
import com.steerinfo.meterwork.meterworkstorageactual.service.IMeterWorkStorageActualService;
import com.steerinfo.pretrack.prestoragescale.model.PreStorageScale;
import com.steerinfo.util.CommonPage;
import com.steerinfo.util.FieldsCollector;
import com.steerinfo.util.PublicMethod;
import com.steerinfo.util.StringUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

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

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

    @Autowired
    IMeterWorkStorageActualService meterWorkStorageActualService;

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

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

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


    @ApiOperation(value = "数据审核", notes = "根据MeterWorkStorageActual对象审核")
    @ApiImplicitParam(name = "MeterWorkStorageActual", value = "详细实体MeterWorkStorageActual", required = true, dataType = "MeterWorkStorageActual")
    //@RequiresPermissions("meterworkcaractualfirst:create")
    @PostMapping(value = "/audit")
    public RESTfulResult audit(@RequestBody MeterWorkStorageActual model) {
        RESTfulResult rm = failed();
        try {
            int num = meterWorkStorageActualService.audit(model);
            if (num >= 1) {
                return success(num);
            } else {
                return failed("操作失败");
            }
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        } finally {

        }
        return rm;
    }

    @ApiOperation(value = "取消审核", notes = "根据MeterWorkStorageActual对象审核")
    @ApiImplicitParam(name = "MeterWorkStorageActual", value = "详细实体MeterWorkStorageActual", required = true, dataType = "MeterWorkStorageActual")
    //@RequiresPermissions("meterworkcaractualfirst:create")
    @PostMapping(value = "/unaudit")
    public RESTfulResult unaudit(@RequestBody MeterWorkStorageActual model) {
        RESTfulResult rm = failed();
        try {
            int num = meterWorkStorageActualService.unaudit(model);
            if (num >= 1) {
                return success(num);
            } else {
                return failed("操作失败");
            }
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        } finally {

        }
        return rm;
    }

    @ApiOperation(value = "条件查询历史", notes = "根据传入参数进行查询")
    @GetMapping(value = "/selectView")
    public RESTfulResult selectView(@RequestParam HashMap parmas, Integer pageNum, Integer pageSize) {
        RESTfulResult rm = failed();
        try {
            PageList<MeterWorkStorageActual> pageInfo = meterWorkStorageActualService.selectView(parmas, pageNum, pageSize);
            return success(pageInfo);
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        } finally {

        }
        return rm;
    }

    @ApiOperation(value = "计量员绩效分析", notes = "根据传入参数进行查询")
    @GetMapping(value = "/analysis")
    public RESTfulResult analysis(@RequestParam String startTime, String endTime, Integer pageNum, Integer pageSize) {
        RESTfulResult rm = failed();
        try {
            PageList<StaffPerformanceAnalysis> pageInfo = meterWorkStorageActualService.analysis(startTime, endTime, pageNum, pageSize);
            return success(pageInfo);
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        } finally {

        }
        return rm;
    }

    @ApiOperation(value = "C#端入库秤实绩", notes = "根据MeterWorkStorageActual对象创建")
    @ApiImplicitParam(name = "meterWorkStorageActual", value = "详细实体meterWorkStorageActual", required = true, dataType = "MeterWorkStorageActual")
    //@RequiresPermissions("meterworkstorageactual:create")
    @PostMapping(value = "/doAddWf")
    public RESTfulResult doAddWf(@RequestBody MeterWorkStorageActual model) {
        try {
            String no = meterWorkStorageActualService.doAddWf(model);
            return success(no);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "C#端入库秤实绩", notes = "根据MeterWorkStorageActual对象创建")
    @ApiImplicitParam(name = "meterWorkStorageActual", value = "详细实体meterWorkStorageActual", required = true, dataType = "MeterWorkStorageActual")
    //@RequiresPermissions("meterworkstorageactual:create")
    @PostMapping(value = "/doQueryWf")
    public RESTfulResult doQueryWf(@RequestBody MeterWorkStorageActual model) {
        try {
            List<MeterWorkStorageActual> no = meterWorkStorageActualService.doQueryWf(model);
            return success(no);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "C#端入库秤实绩", notes = "根据MeterWorkStorageActual对象创建")
    @ApiImplicitParam(name = "meterWorkStorageActual", value = "详细实体meterWorkStorageActual", required = true, dataType = "MeterWorkStorageActual")
    //@RequiresPermissions("meterworkstorageactual:create")
    @PostMapping(value = "/doQueryTimeWf")
    public RESTfulResult doQueryTimeWf(@RequestBody CommonPage<MeterWorkStorageActual> model) {
        try {
            List<MeterWorkStorageActual> no = meterWorkStorageActualService.doQueryTimeWf(model);
            return success(no);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "C#端入作废及还原数据", notes = "根据MeterWorkStorageActual对象创建")
    @ApiImplicitParam(name = "meterWorkStorageActual", value = "详细实体meterWorkStorageActual", required = true, dataType = "MeterWorkStorageActual")
    @PostMapping(value = "/doUpdateWf")
    public RESTfulResult doUpdateWf(@RequestBody List<MeterWorkStorageActual> model) {
        try {
            int no = meterWorkStorageActualService.doUpdateWf(model);
            return success(no);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }


    @Transactional(rollbackFor = Exception.class)
    @ApiOperation(value = "C#端热送磅", notes = "根据MeterWorkStorageActual对象创建")
    @ApiImplicitParam(name = "meterWorkStorageActual", value = "详细实体meterWorkStorageActual", required = true, dataType = "MeterWorkStorageActual")
    //@RequiresPermissions("meterworkstorageactual:create")
    @PostMapping(value = "/doAddHotDeliveryBelt")
    public RESTfulResult doAddHotDeliveryBelt(@RequestBody MeterWorkStorageActual model) {
        RESTfulResult rm = failed();
        try {
            String str = meterWorkStorageActualService.doAddHotDeliveryBelt(model);
            if (StringUtils.isNotEmpty(str) && str.startsWith("20")) {
                return success(str);
            } 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 = "界面填写数据，服务端做二次校验及赋值")
    @Transactional(rollbackFor = {MarkerMetException.class, Exception.class})
    @PostMapping(value = "/doUpdateClient")
    public RESTfulResult doUpdateClient(@RequestBody List<MeterWorkStorageActual> models) {
        RESTfulResult rm = failed();
        try {
            String str = meterWorkStorageActualService.doUpdateClient(models);
            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 = "界面填写数据")
    @Transactional(rollbackFor = {MarkerMetException.class, Exception.class})
    @PostMapping(value = "/doUpdateClientXH")
    public RESTfulResult doUpdateClientXH(@RequestBody List<MeterWorkStorageActual> models) {
        RESTfulResult rm = failed();
        try {
            String str = meterWorkStorageActualService.doUpdateClientXH(models);
            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 = "C#端热送磅分组数据查询", notes = "根据MeterWorkStorageActual对象创建")
    @ApiImplicitParam(name = "meterWorkStorageActual", value = "详细实体meterWorkStorageActual", required = true, dataType = "MeterWorkStorageActual")
    //@RequiresPermissions("meterworkstorageactual:create")
    @PostMapping(value = "/doQueryGroup")
    public RESTfulResult doQueryGroup(@RequestBody MeterWorkStorageActual model) {
        try {
            HashMap params = (HashMap) FieldsCollector.beanToMap(model);
            if(model.getStartTime() !=null && model.getEndTime()!=null ){
                params.put("startTime", PublicMethod.dateToStringSS(model.getStartTime()));
                params.put("endTime", PublicMethod.dateToStringSS(model.getEndTime()));
            }
            if(model.getCreateTime1() !=null && model.getCreateTime2()!=null ){
                params.put("createTime1", PublicMethod.dateToStringSS(model.getCreateTime1()));
                params.put("createTime2", PublicMethod.dateToStringSS(model.getCreateTime2()));
            }
            List<MeterWorkStorageActual> no = meterWorkStorageActualService.doQueryGroup(params);
            return success(no);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }


    @ApiOperation(value = "C#端热送磅审核数据查询", notes = "根据MeterWorkStorageActual对象创建")
    @ApiImplicitParam(name = "meterWorkStorageActual", value = "详细实体meterWorkStorageActual", required = true, dataType = "MeterWorkStorageActual")
    //@RequiresPermissions("meterworkstorageactual:create")
    @PostMapping(value = "/doQueryCheckCount")
    public RESTfulResult doQueryCheckCount(@RequestBody MeterWorkStorageActual model) {
        try {
            model.setValueFlag("1");//默认查询有效的数据
            HashMap params = (HashMap) FieldsCollector.beanToMap(model);
            if(model.getStartTime() !=null && model.getEndTime()!=null ){
                params.put("startTime", PublicMethod.dateToStringSS(model.getStartTime()));
                params.put("endTime", PublicMethod.dateToStringSS(model.getEndTime()));
            }
            if(model.getCreateTime1() !=null && model.getCreateTime2()!=null ){
                params.put("createTime1", PublicMethod.dateToStringSS(model.getCreateTime1()));
                params.put("createTime2", PublicMethod.dateToStringSS(model.getCreateTime2()));
            }
//            params.put("createTime1", PublicMethod.dateToStringSS(model.getCreateTime1));
//            params.put("createTime2", PublicMethod.dateToStringSS(model.getCreateTime1));
            List<MeterWorkStorageActual> no = meterWorkStorageActualService.doQueryCheckCount(params);
            return success(no);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }
}
