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

import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.produce.manage.model.*;
import com.deer.wms.produce.manage.service.ProductProcessBomService;
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.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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

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


/**
* Created by guo on 2019/07/18.
*/
@Api(description = "产品bom接口")
@RestController
@RequestMapping("/product/process/boms")
public class ProductProcessBomController {

    @Autowired
    private ProductProcessBomService productProcessBomService;

    @OperateLog(description = "添加xxx", type = "增加")
    @ApiOperation(value = "添加xxx", notes = "添加xxx")
    @PostMapping("/add")
    public Result add(@RequestBody ProductProcessBom mtAloneProductProcessBom, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult( CommonCode.SERVICE_ERROR,"未登录错误",null );
        }
        mtAloneProductProcessBom.setCreateTime(new Date());
        mtAloneProductProcessBom.setCompanyId(currentUser.getCompanyId());
        productProcessBomService.save(mtAloneProductProcessBom);
        return ResultGenerator.genSuccessResult();
    }

    @OperateLog(description = "添加工艺", type = "增加")
    @ApiOperation(value = "添加工艺", notes = "添加工艺")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", required = true)
    })
    @PostMapping("/add/product/process/bom")
    public Result addProductProcessBom(@RequestBody ProductProcessBomParams productProcessBomParams,
                       @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult( CommonCode.SERVICE_ERROR,"未登录错误",null );
        }
        productProcessBomParams.setCreateTime(new Date());
        productProcessBomParams.setCompanyId(currentUser.getCompanyId());
//        productProcessBomParams.setProductId(1);
//        productProcessBomParams.setProcessContent("1");
//        productProcessBomParams.setSpecification("1");
        System.out.println(productProcessBomParams.toString());
        productProcessBomService.addProductProcessBom(productProcessBomParams);
        return ResultGenerator.genSuccessResult();
    }

    @OperateLog(description = "修改工艺", type = "修改")
    @ApiOperation(value = "修改工艺", notes = "修改工艺")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", required = true)
    })
    @PostMapping("/update/product/process/bom")
    public Result updateProductProcessBom(@RequestBody ProductProcessBomParams productProcessBomParams,
                                       @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult( CommonCode.SERVICE_ERROR,"未登录错误",null );
        }
        productProcessBomParams.setCreateTime(new Date());
        productProcessBomParams.setCompanyId(currentUser.getCompanyId());
        productProcessBomService.updateProductProcessBom(productProcessBomParams);
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "删除工艺", notes = "删除工艺")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", required = true)
    })
    @PostMapping("/delete/product/process/bom")
    public Result deleteProductProcessBom(@RequestBody ProductProcessBomParams productProcessBomParams,
                                          @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult( CommonCode.SERVICE_ERROR,"未登录错误",null );
        }
        productProcessBomParams.setCreateTime(new Date());
        productProcessBomParams.setCompanyId(currentUser.getCompanyId());
        productProcessBomService.updateProductProcessBom(productProcessBomParams);
        return ResultGenerator.genSuccessResult();
    }

    
    @OperateLog(description = "删除xxx", type = "删除")
    @ApiOperation(value = "删除xxx", notes = "删除xxx")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        productProcessBomService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }
    
    @OperateLog(description = "修改xxx", type = "更新")
    @ApiOperation(value = "修改xxx", notes = "修改xxx")
    @PostMapping("/update")
    public Result update(@RequestBody ProductProcessBom mtAloneProductProcessBom) {
        mtAloneProductProcessBom.setUpdateTime(new Date());
        productProcessBomService.update(mtAloneProductProcessBom);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable Integer id) {
        ProductProcessBom productProcessBom = productProcessBomService.findById(id);
        return ResultGenerator.genSuccessResult(productProcessBom);
    }

    /**仅获取所有产品Bom名称、产品名称、公司名称，用于新建加工单页面产品下拉框，该下拉框中的产品是已有bom的产品**/
    @GetMapping("/proBomAndProList")
    public Result proBomAndProList(ProductProcessBomParams 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<ProductProcessBomDto> list = productProcessBomService.findProBomAndProList(params);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("/list/by/term")
    public Result listByTerm(ProductProcessBomParams 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<ProductProcessBomDto> list = productProcessBomService.findListByTerm(params);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    /**
     * hy
     * 根据产品bom的id获取产品bom,用于新建批次产品加工的产品bom下拉框填入名称
     * @param params
     * @param currentUser
     * @return
     */
    @GetMapping("/listById")
    public Result listByProductId(ProductProcessBomParams 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<ProductProcessBom> list = productProcessBomService.findListById(params);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    /**
     * hy暂时没用
     * 根据产品bom的id获取产品bom、工序bom、物料bom信息
     * @param params
     * @param currentUser
     * @return
     */
    @GetMapping("/findAllBomByProdProcBom")
    public Result findAllBomByProdProcBom(ProductProcessBomParams params, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR,"未登录错误",null );
        }

        params.setCompanyId(currentUser.getCompanyId());
        ProductProcessBomHo productProcessBomHo = productProcessBomService.findAllBomByProdProcBom(params);
        return ResultGenerator.genSuccessResult(productProcessBomHo);
    }

    /**
     * author:caixiang
     *
     * desc:依据产品名称、产品编号查询该产品的工艺列表
     **/
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", required = true)
    })
    @ApiOperation(value = "依据产品名称、产品编号查询该产品的工艺列表", notes = "依据产品名称、产品编号查询该产品的工艺列表")
    @PostMapping("/selectProcessByProductNameNum")
    public Result selectProcessByProductNameNum(@RequestBody ProductProcessBomParams mtAloneProductProcessBomParams, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult( CommonCode.SERVICE_ERROR,"未登录错误",null );
        }
        mtAloneProductProcessBomParams.setCompanyId(currentUser.getCompanyId());
        PageHelper.startPage(mtAloneProductProcessBomParams.getPageNum(), mtAloneProductProcessBomParams.getPageSize());
        List<ProductProcessBomVO> byProductNameAndProductNum = new ArrayList<ProductProcessBomVO>();
        try {
            System.out.println("pageNum:"+mtAloneProductProcessBomParams.getPageNum());
            System.out.println("getPageSize:"+mtAloneProductProcessBomParams.getPageSize());
            byProductNameAndProductNum = productProcessBomService.findByProductNameAndProductNum(mtAloneProductProcessBomParams);

        }catch (Exception e){
            ResultGenerator.genFailResult(CommonCode.SERVER_INERNAL_ERROR,"操作数据库异常","null");
        }
        PageInfo pageInfo = new PageInfo(byProductNameAndProductNum);;
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    /**
     * why
     * 根据产品bom的id查询出来所对应的数据
     */
    @GetMapping("/find/data/by/{id}")
    @ApiOperation(value = "依据产品bom的id查询该产品的工艺列表", notes = "依据产品bom的id查询该产品的工艺列表")
    public Result findDataById(@PathVariable Integer id) {
        ProductProcessBomDto productProcessBomDto = productProcessBomService.findDataById(id);
        return ResultGenerator.genSuccessResult(productProcessBomDto);
    }
}
