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.ProcessBomService;
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.Date;
import java.util.List;


/**
* Created by guo on 2019/07/18.
*/
@Api(description = "工序Bom接口")
@RestController
@RequestMapping("/process/bom")
public class ProcessBomController {

    @Autowired
    private ProcessBomService processBomService;

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

    @OperateLog(description = "添加产品工艺", type = "增加")
    @ApiOperation(value = "添加产品工艺", notes = "添加产品工艺")
    @PostMapping("/add/process/to/product")
    public Result addProcessToProduct(@RequestBody ProcessBomParams params, @ApiIgnore @User CurrentUser currentUser){
        if(currentUser==null){
            return ResultGenerator.genFailResult( CommonCode.SERVICE_ERROR,"未登录错误",null );
        }
        params.setCreateTime(new Date());
        params.setCompanyId(currentUser.getCompanyId());
        List<ProcessBomVo> list = params.getProcessBomVoList();
        for (ProcessBomVo processBomVo:list) {
            processBomVo.setCreateTime(new Date());
            processBomVo.setCompanyId(currentUser.getCompanyId());
            for (ProcessMaterialsBom processMaterialsBom:processBomVo.getList()) {
                processMaterialsBom.setCreateTime(new Date());
                processMaterialsBom.setCompanyId(currentUser.getCompanyId());
            }
        }
        processBomService.addProcessToProduct(params);
        return ResultGenerator.genSuccessResult();
    }
    
    @OperateLog(description = "删除xxx", type = "删除")
    @ApiOperation(value = "删除xxx", notes = "删除xxx")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        processBomService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }
    
    @OperateLog(description = "修改xxx", type = "更新")
    @ApiOperation(value = "修改xxx", notes = "修改xxx")
    @PostMapping("/update")
    public Result update(@RequestBody ProcessBom processBom) {

        processBomService.update(processBom);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable Integer id) {
        ProcessBom mtAloneProcessBom = processBomService.findById(id);
        return ResultGenerator.genSuccessResult(mtAloneProcessBom);
    }

    @GetMapping("/list")
    public Result list(ProcessBomParams 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<ProcessBom> list = processBomService.findList(params);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("/list/by/id")
    public Result listById(ProcessBomParams 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<ProcessBom> list = processBomService.findListById(params);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    /**
     * hy弃用
     * 根据选择的产品名称来展示产品其他数据和工序数据,
     * 用于新建生产加工产品bom下拉框选择
     * @param params
     * @param currentUser
     * @return
     */
    @ApiOperation(value = "根据产品名称获取工序", notes = "根据产品名称获取工序")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", required = true)
    })
    @GetMapping("/list/by/product/name/id")
    public Result listByProductNameId(ProcessBomParams 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<ProcessBomDto> list = processBomService.findListByProductNameId(params);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    /**
     * hy正在用
     * 根据产品bom的id获取产品bom部分数据和工序数据
     * 用于新建生产加工产品bom下拉框选择
     * @param params
     * @param currentUser
     * @return
     */
    @ApiOperation(value = "根据产品bom的id获取产品bom部分数据和工序数据", notes = "根据产品bom的id获取产品bom部分数据和工序数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productProcessId", value = "产品bom的id", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(name = "companyId", value = "公司id", paramType = "query", dataType = "int", required = false)
    })
    @GetMapping("/and/product/bom/by/prodbom")
    public Result ProductAndProcessBomByProdBom(ProcessBomParams params, @ApiIgnore @User CurrentUser currentUser){
        //if(currentUser==null){
        //    return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR,"未登录错误",null );
        //}

        params.setCompanyId(1);
        ProductProcessBomPo productProcessBomPo = processBomService.findProductAndProcessBomByProdBom(params);
        return ResultGenerator.genSuccessResult(productProcessBomPo);
    }

    /**
     * 添加工序数据到数据库
     */
    @ApiOperation(value = "工序来获取物料信息", notes = "工序来获取物料信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", required = true)
    })
    @GetMapping("/insert/process/bom")
    public Result insertProcessBom(ProcessBomParams 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);
        }

        return ResultGenerator.genSuccessResult();
    }

    /**
     * hy
     * 根据产品bom的id获取工序bom*
     * @param params
     * @param currentUser
     * @return
     */
    @GetMapping("/listByProductBom")
    public Result listByProductBom(ProcessBomParams 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<ProcessBom> list = processBomService.findListByProductBom(params);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
