package com.deer.wms.produce.manage.web;

import com.deer.wms.produce.manage.model.ProductProcessBatchDetParams;
import com.deer.wms.produce.manage.model.ProductProcessParams;
import com.deer.wms.project.seed.annotation.OperateLog;
import com.deer.wms.project.seed.constant.SystemManageConstant;
import com.deer.wms.project.seed.core.result.CommonCode;
import com.deer.wms.project.seed.core.result.Result;
import com.deer.wms.project.seed.core.result.ResultGenerator;
import com.deer.wms.produce.manage.model.ProductProcessBatchDet;
import com.deer.wms.produce.manage.service.ProductProcessBatchDetService;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List; 



/**
* Created by  on 2019/09/09.
*/
@Api(description = "批次明细接口")
@RestController
@RequestMapping("/product/process/batch/dets")
public class ProductProcessBatchDetController {

    @Autowired
    private ProductProcessBatchDetService productProcessBatchDetService;

    @OperateLog(description = "添加xxx", type = "增加")
    @ApiOperation(value = "添加xxx", notes = "添加xxx")
    @PostMapping("/add")
    public Result add(@RequestBody ProductProcessBatchDet productProcessBatchDet, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult( CommonCode.SERVICE_ERROR,"未登录错误",null );
        }
        productProcessBatchDet.setCreateTime(new Date());
        productProcessBatchDet.setCompanyId(currentUser.getCompanyId());
        productProcessBatchDetService.save(productProcessBatchDet);
        return ResultGenerator.genSuccessResult();
    }
    
    @OperateLog(description = "删除xxx", type = "删除")
    @ApiOperation(value = "删除xxx", notes = "删除xxx")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        productProcessBatchDetService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }
    
    @OperateLog(description = "修改xxx", type = "更新")
    @ApiOperation(value = "修改xxx", notes = "修改xxx")
    @PostMapping("/update")
    public Result update(@RequestBody ProductProcessBatchDet productProcessBatchDet) {
        productProcessBatchDet.setUpdateTime(new Date());
        productProcessBatchDetService.update(productProcessBatchDet);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable Integer id) {
        ProductProcessBatchDet productProcessBatchDet = productProcessBatchDetService.findById(id);
        return ResultGenerator.genSuccessResult(productProcessBatchDet);
    }

    /**
     * hy正在用（打卷系统）
     * 获取批次加工单下的所有明细
     * @param params
     * @param currentUser
     * @return
     */
    @ApiOperation(value = "批次生产加工单下的明细查询", notes = "批次生产加工单下的明细查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productProcessId", value = "批次生产加工单id", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(name = "companyId", value = "公司id", paramType = "query", dataType = "int", required = false)
    })
    @OperateLog(description = "批次生产加工单下的明细查询", type = "查询")
    @GetMapping("/listByProductProcess")
    public Result listByProductProcess(ProductProcessParams params, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR,"未登录错误",null );
        }

        params.setCompanyId(currentUser.getCompanyId());
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<ProductProcessBatchDet> list = productProcessBatchDetService.findListByProductProcess(params);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }


}
