package com.zhongzheng.controller.goods;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import com.zhongzheng.modules.bank.bo.ExamQuestionQueryBo;
import com.zhongzheng.modules.bank.vo.ExamQuestionVo;
import com.zhongzheng.modules.exam.bo.ExamNumberGoodsQueryBo;
import com.zhongzheng.modules.exam.vo.ExamNumberGoodsVo;
import com.zhongzheng.modules.goods.bo.*;
import com.zhongzheng.modules.goods.domain.Goods;
import com.zhongzheng.modules.goods.service.IGoodsAttachedService;
import com.zhongzheng.modules.goods.service.IGoodsAuditionConfigService;
import com.zhongzheng.modules.goods.service.IGoodsService;
import com.zhongzheng.modules.goods.vo.GoodsAttachedVo;
import com.zhongzheng.modules.goods.vo.GoodsAuditionConfigVo;
import com.zhongzheng.modules.goods.vo.GoodsVo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhongzheng.common.annotation.Log;
import com.zhongzheng.common.core.controller.BaseController;
import com.zhongzheng.common.core.domain.AjaxResult;
import com.zhongzheng.common.enums.BusinessType;
import com.zhongzheng.common.utils.poi.ExcelUtil;
import com.zhongzheng.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 商品Controller
 *
 * @author hjl
 * @date 2021-10-12
 */
@Api(value = "商品控制器", tags = {"商品管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/goods")
public class GoodsController extends BaseController {

    private final IGoodsService iGoodsService;

    private final IGoodsAttachedService iGoodsAttachedService;

    @Autowired
    private IGoodsAuditionConfigService iGoodsAuditionConfigService;

    /**
     * 查询商品列表
     */
    @ApiOperation("查询商品列表")
    @PreAuthorize("@ss.hasPermi('system:goods:list')")
    @GetMapping("/list")
    public TableDataInfo<GoodsVo> list(GoodsQueryBo bo) {
      /*  bo.setStatus(new ArrayList<Integer>(){{
            add(1);
        }});*/
        startPage();
        List<GoodsVo> list = iGoodsService.selectList(bo);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    /*selectList*/

    /**
     * 获取商品详细信息
     */
    @ApiOperation("获取商品详细信息")
    @PreAuthorize("@ss.hasPermi('system:goods:query')")
    @GetMapping("/{goodsId}")
    public AjaxResult<GoodsVo> getInfo(@PathVariable("goodsId" ) Long goodsId) {
        return AjaxResult.success(iGoodsService.selectDetail(goodsId));
    }

    /**
     * 新增课程商品
     */
    @ApiOperation("新增课程商品")
    @PreAuthorize("@ss.hasPermi('system:goods:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Void> add(@RequestBody GoodsAddBo bo) {
        return toAjax(iGoodsService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改商品
     */
    @ApiOperation("修改商品")
    @PreAuthorize("@ss.hasPermi('system:goods:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult<Void> edit(@RequestBody GoodsEditBo bo) {
        return toAjax(iGoodsService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除商品
     */
   /* @ApiOperation("删除商品")
    @PreAuthorize("@ss.hasPermi('system:goods:remove')")
    @Log(title = "商品" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{goodsIds}")
    public AjaxResult<Void> remove(@PathVariable Long[] goodsIds) {
        return toAjax(iGoodsService.deleteWithValidByIds(Arrays.asList(goodsIds), true) ? 1 : 0);
    }*/

    /**
     * 新增题库商品
     */
    @ApiOperation("新增题库商品")
    @PreAuthorize("@ss.hasPermi('system:goods:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping("/bank")
    public AjaxResult<Void> addBank(@RequestBody GoodsBankAddBo bo) {
        return toAjax(iGoodsService.insertBankByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改商品
     */
    @ApiOperation("修改题库商品")
    @PreAuthorize("@ss.hasPermi('system:goods:edit')")
    @Log(title = "题库商品", businessType = BusinessType.UPDATE)
    @PostMapping("/bank/edit")
    public AjaxResult<Void> editBank(@RequestBody GoodsBankEditBo bo) {
        return toAjax(iGoodsService.updateBankByEditBo(bo) ? 1 : 0);
    }

    /**
     * 查询题目业务层次关系列表
     */
    @ApiOperation("查询题库商品试卷列表")
    @PreAuthorize("@ss.hasPermi('system:business:list')")
    @GetMapping("/bank/list")
    public AjaxResult<List<GoodsAttachedVo>> bankList(GoodsAttachedQueryBo bo) {
        List<GoodsAttachedVo> list = iGoodsAttachedService.selectList(bo);
        return AjaxResult.success(list);
    }


    /**
     * 新增课程商品
     */
    @ApiOperation("新增补考商品")
    @PreAuthorize("@ss.hasPermi('system:goods:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping("/make")
    public AjaxResult<Void> addMake(@RequestBody GoodsAddBo bo) {
        return toAjax(iGoodsService.insertMakeByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改商品
     */
    @ApiOperation("修改补考商品")
    @PreAuthorize("@ss.hasPermi('system:goods:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/make/edit")
    public AjaxResult<Void> editMake(@RequestBody GoodsEditBo bo) {
        return toAjax(iGoodsService.updateMakeByEditBo(bo) ? 1 : 0);
    }


    /**
     * 新增课程商品
     */
    @ApiOperation("新增前培商品")
    @PreAuthorize("@ss.hasPermi('system:goods:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping("/front")
    public AjaxResult<Void> addFront(@RequestBody GoodsAddBo bo) {
        return toAjax(iGoodsService.insertFrontByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改商品
     */
    @ApiOperation("修改前培商品")
    @PreAuthorize("@ss.hasPermi('system:goods:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/front/edit")
    public AjaxResult<Void> editFront(@RequestBody GoodsEditBo bo) {
        return toAjax(iGoodsService.updateFrontByEditBo(bo) ? 1 : 0);
    }

    /**
     * 查询考试配置绑定商品列表
     */
    @ApiOperation("查询前培或补考商品列表")
    @PreAuthorize("@ss.hasPermi('system:goods:list')")
    @GetMapping("/listGoods")
    public TableDataInfo<ExamNumberGoodsVo> listGoods(ExamNumberGoodsQueryBo bo) {
        startPage();
        List<ExamNumberGoodsVo> list = iGoodsService.listGoods(bo);
        return getDataTable(list);
    }
}
