package com.ym.mec.web.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.entity.GoodsCategory;
import com.ym.mec.biz.dal.entity.GoodsProcurement;
import com.ym.mec.biz.dal.enums.AccountType;
import com.ym.mec.biz.service.GoodsCategoryService;
import com.ym.mec.common.entity.HttpResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Arrays;
import java.util.Date;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.ym.mec.biz.dal.entity.Goods;
import com.ym.mec.biz.dal.enums.YesOrNoEnum;
import com.ym.mec.biz.dal.page.GoodsQueryInfo;
import com.ym.mec.biz.service.GoodsService;
import com.ym.mec.common.controller.BaseController;

@RequestMapping("goods")
@Api(tags = "商品(教材、辅件)服务")
@RestController
public class GoodsController extends BaseController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsCategoryService goodsCategoryService;
    @Autowired
    private SysUserFeignService sysUserFeignService;

    @ApiOperation(value = "新增商品(教材、辅件)")
    @PostMapping("/add")
    @PreAuthorize("@pcs.hasPermissions('goods/add')")
    public Object add(Goods goods){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        goodsService.addGoods(goods,sysUser.getId());
        return succeed();
    }

    @ApiOperation(value = "新增商品清单")
    @PostMapping("/addGoodsProcurement")
    @PreAuthorize("@pcs.hasPermissions('goods/addGoodsProcurement')")
    public Object addGoodsProcurement(GoodsProcurement goodsProcurement){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        goodsProcurement.setOperatorId(sysUser.getId());
        goodsService.addGoodsProcurement(goodsProcurement);
        return succeed();
    }

    @ApiOperation(value = "删除商品(教材、辅件)")
    @PostMapping("/del/{id}")
    @PreAuthorize("@pcs.hasPermissions('goods/del')")
    public Object del(@ApiParam(value = "商品(教材、辅件)编号", required = true) @PathVariable("id") Integer id){
    	Goods goods = goodsService.get(id);
    	if(goods == null){
    		return failed("参数错误");
    	}
    	goods.setStatus(YesOrNoEnum.NO);
    	goods.setUpdateTime(new Date());
        goodsService.update(goods);
        return succeed();
    }

    @ApiOperation(value = "修改商品件状态")
    @PostMapping("/updateGoodsStatus")
    @PreAuthorize("@pcs.hasPermissions('goods/updateGoodsStatus')")
    public Object updateGoodsStatus(Integer goodsId, Integer status) {
        goodsService.updateGoodsStatus(goodsId, status);
        return succeed();
    }

    @ApiOperation(value = "修改商品(教材、辅件)")
    @PostMapping("/update")
    @PreAuthorize("@pcs.hasPermissions('goods/update')")
    public Object update(Goods goods){
        goods.setUpdateTime(new Date());
        goodsService.update(goods);
        return succeed();
    }

    @ApiOperation(value = "根据商品(教材、辅件)编号查询商品(教材、辅件)")
    @GetMapping("/get/{id}")
    public Object get(@ApiParam(value = "商品(教材、辅件)编号", required = true) @PathVariable("id") Integer id){
        Goods goods = goodsService.get(id);
        GoodsCategory goodsCategory = goodsCategoryService.get(goods.getGoodsCategoryId());
        if(Objects.nonNull(goodsCategory)){
            goods.setGoodsCategoryName(goodsCategory.getName());
        }
        return succeed(goods);
    }

    @ApiOperation(value = "分页查询商品(教材、辅件)列表")
    @GetMapping("/queryPage")
    @PreAuthorize("@pcs.hasPermissions('goods/queryPage')")
    public Object queryPage(GoodsQueryInfo queryInfo){
        return succeed(goodsService.queryPage(queryInfo));
    }

    @ApiOperation(value = "通过科目编号、商品分类 查询商品(教材、辅件)列表")
    @GetMapping("/queryGoodsBySubId")
    @PreAuthorize("@pcs.hasPermissions('goods/queryGoodsBySubId')")
    @ApiImplicitParams({ @ApiImplicitParam(name = "subjectId", value = "科目编号", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "type", value = "INSTRUMENT 乐器, ACCESSORIES 教辅", required = true, dataType = "String")})
    public Object findGoodsBySubId(Integer subjectId,String type){
        return succeed(goodsService.findGoodsBySubId(subjectId,type));
    }
}
