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

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.MtStockProvendor;
import com.deer.wms.produce.manage.model.MtStockProvendorParams;
import com.deer.wms.produce.manage.service.MtStockProvendorService;
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.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/10/17.
*/
@Api(description = "采购供应商信息接口")
@RestController
@RequestMapping("/mt/stock/provendors")
public class MtStockProvendorController {

    @Autowired
    private MtStockProvendorService mtStockProvendorService;

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

    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable Integer id) {
        MtStockProvendor mtStockProvendor = mtStockProvendorService.findById(id);
        return ResultGenerator.genSuccessResult(mtStockProvendor);
    }

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

}
