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

import com.deer.wms.produce.manage.model.ProductBatchDetectInfo;
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.ProductBatchDetect;
import com.deer.wms.produce.manage.model.ProductBatchDetectParams;
import com.deer.wms.produce.manage.service.ProductBatchDetectService;
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/batch/detects")
public class ProductBatchDetectController {

    @Autowired
    private ProductBatchDetectService productBatchDetectService;

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

    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable Integer id) {
        ProductBatchDetect productBatchDetect = productBatchDetectService.findById(id);
        return ResultGenerator.genSuccessResult(productBatchDetect);
    }

    @GetMapping("/list")
    public Result list(ProductBatchDetectParams params, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR,"未登录错误",null );
        }

    	if (currentUser.getCompanyType() != SystemManageConstant.COMPANY_TYPE_MT){
    		params.setCompanyId(currentUser.getCompanyId());
		}else{
			params.setCompanyId(null);
        }
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<ProductBatchDetect> list = productBatchDetectService.findList(params);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    /**
     * hy正在用（打卷系统）
     * 保存工序和批次明细对应的检测等级和瑕疵名细
     * @param productBatchDetectInfo
     * @param currentUser
     * @return
     */
    @ApiOperation(value = "保存检测等级和瑕疵名细", notes = "保存检测等级和瑕疵名细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productBatchDetectInfo", value = "批次加工单、工序、批次明细、检测等级、瑕疵名细混合对象", paramType = "body", dataType = "ProductBatchDetectInfo", required = true)
    })
    @OperateLog(description = "保存检测等级和瑕疵名细", type = "增加")
    @PostMapping("/saveDetectRusult")
    public Result saveDetectRusult(@RequestBody ProductBatchDetectInfo productBatchDetectInfo, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult( CommonCode.SERVICE_ERROR,"未登录错误",null );
        }

        productBatchDetectService.saveDetectRusult(productBatchDetectInfo, currentUser);
        return ResultGenerator.genSuccessResult();
    }
}
