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

import com.deer.wms.produce.manage.constant.ProduceManageConstant;
import com.deer.wms.produce.manage.constant.ProduceManagePublicMethod;
import com.deer.wms.produce.manage.model.*;
import com.deer.wms.produce.manage.service.MaterialsOutgoingLogService;
import com.deer.wms.produce.manage.service.MaterialsStockInfoService;
import com.deer.wms.project.seed.annotation.OperateLog;
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.service.MaterialsInfoService;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.seed.util.StringUtil;
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.ArrayList;
import java.util.Date;
import java.util.List; 



/**
* Created by  on 2019/07/18.
*/
@Api(description = "物料接口")
@RestController
@RequestMapping("/materials/infos")
public class MaterialsInfoController {

    @Autowired
    private MaterialsInfoService materialsInfoService;

    /**
     * hy正在用
     * 新增物料信息
     * @param materialsInfo
     * @param currentUser
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "materialsInfo", value = "物料", paramType = "body", dataType = "MaterialsInfo", required = true)
    })
    @OperateLog(description = "添加物料信息", type = "增加")
    @ApiOperation(value = "添加物料信息", notes = "添加物料信息")
    @PostMapping("/add")
    public Result add(@RequestBody MaterialsInfo materialsInfo, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult( CommonCode.SERVICE_ERROR,"未登录错误",null );
        }

        Date date = new Date();
        materialsInfo.setCreateTime(date);
        materialsInfo.setVersion("1.1");
        materialsInfo.setStatus(ProduceManageConstant.STATUS_AVAILABLE);
        materialsInfo.setOperatorId(currentUser.getUserId());
        materialsInfo.setCompanyId(currentUser.getCompanyId());
        materialsInfo.setCode(ProduceManagePublicMethod.creatUniqueCode("WL"));
        materialsInfoService.save(materialsInfo);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * hy正在用
     * 删除物料，顺带删除相应的库存信息、出入库记录
     * @param materialsId
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "materialsId", value = "物料id", paramType = "query", dataType = "int", required = true)
    })
    @OperateLog(description = "删除物料", type = "删除")
    @ApiOperation(value = "删除物料", notes = "删除物料")
    @DeleteMapping("/delete")
    public Result delete(Integer materialsId, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR,"未登录错误",null );
        }

        materialsInfoService.deleteRelevantById(materialsId, currentUser);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * hy正在用
     * 物料更新
     * @param materialsInfo
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "materialsInfo", value = "物料", paramType = "body", dataType = "MaterialsInfo", required = true)
    })
    @OperateLog(description = "修改物料", type = "更新")
    @ApiOperation(value = "修改物料", notes = "修改物料")
    @PostMapping("/update")
    public Result update(@RequestBody MaterialsInfo materialsInfo) {
        materialsInfoService.update(materialsInfo);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * hy正在用
     * 获取某个物料详细信息
     * @param params
     * @param currentUser
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "公司id", paramType = "query", dataType = "int", required = false),
            @ApiImplicitParam(name = "materialsId", value = "物料id", paramType = "query", dataType = "int", required = true)
    })
    @OperateLog(description = "获取某种物料详细信息", type = "查询")
    @ApiOperation(value = "获取某种物料详细信息", notes = "获取某种物料详细信息")
    @GetMapping("/detail")
    public Result detail(MaterialsInfoParams params, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR,"未登录错误",null );
        }
        StringUtil.trimObjectStringProperties(params);

        params.setCompanyId(currentUser.getCompanyId());
        MaterialsInfoDto materialsInfoDto = materialsInfoService.findDetailById(params);
        return ResultGenerator.genSuccessResult(materialsInfoDto);
    }

    /**
     * hy正在用
     * 物料信息分页列表查询
     * @param params
     * @param currentUser
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "公司id", paramType = "query", dataType = "int", required = false),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "begDate", value = "起始日期", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "endDate", value = "结束日期", paramType = "query", dataType = "String", required = false)
    })
    @OperateLog(description = "物料信息分页查询", type = "查询")
    @ApiOperation(value = "物料信息列表分页", notes = "物料信息列表分页")
    @GetMapping("/list")
    public Result list(MaterialsInfoParams params, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR,"未登录错误",null );
        }
        StringUtil.trimObjectStringProperties(params);

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

    /**
     * author:caixiang
     *
     * desc:依据物料名称、编号查询该物料的bom
     **/
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", required = true)
    })
    @ApiOperation(value = "依据材料名称或者num查找物料bom", notes = "依据材料名称或者num查找物料bom")
    @PostMapping("/selectMaterialByNameOrNum")
    public Result selectMaterialByNameOrNum(@RequestBody MaterialsInfoParams 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<MaterialsInfoVO> list = new ArrayList<>();
        try {
            list = materialsInfoService.findMaterialByNameAndNum(params);
        }catch (Exception e){
            ResultGenerator.genFailResult(CommonCode.SERVER_INERNAL_ERROR,"操作数据库异常","null");
        }
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    /**
     * author:caixiang
     *
     * desc: 依据材料关键字 查找对用的list。
     * 关键字(其中一个关键字)：
     *      mt_alone_materials_info（materials_name OR code OR specification OR unit_id）
     *      mt_alone_process_materials_bom(dosage)
     **/
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", required = true)
    })
    @ApiOperation(value = "依据材料关键字 查找对用的list", notes = "依据材料关键字 查找对用的list")
    @PostMapping("/selectMaterialsByKeyWords")
    public Result selectMaterialsByKeyWords(@RequestBody MaterialsInfoParams params, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult( CommonCode.SERVICE_ERROR,"未登录错误",null );
        }
        System.err.println("pageSize："+params.getPageSize()+"，，pageNum："+params.getPageNum()+",keyWords"+params.getKeywords());
        params.setCompanyId(currentUser.getCompanyId());
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<MaterialsInfoVO> list = new ArrayList<>();
        try {
            list = materialsInfoService.findMaterialsByKeyWords(params);
        }catch (Exception e){
            ResultGenerator.genFailResult(CommonCode.SERVER_INERNAL_ERROR,"操作数据库异常","null");
        }
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

}
