package com.yiqi.admin.controller.core;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.yiqi.common.annotation.Log;
import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.common.core.domain.AjaxResult;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.core.domain.model.LoginUser;
import com.yiqi.common.core.page.TableDataInfo;
import com.yiqi.common.enums.BusinessType;
import com.yiqi.common.utils.poi.ExcelUtil;
import com.yiqi.core.domain.GoodsInfo;
import com.yiqi.core.domain.GoodsSku;
import com.yiqi.core.domain.GoodsSkuSpec;
import com.yiqi.core.domain.vo.GoodsSkuAddVO;
import com.yiqi.core.domain.vo.GoodsSkuQueryVO;
import com.yiqi.core.service.IGoodsSkuService;
import com.yiqi.order.domain.dto.RetailQueryGoodsDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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 javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 物料明细Controller
 *
 * @author ruoyi
 * @date 2023-08-21
 */
@Api(tags = "管理后台 - 物料明细")
@RestController
@RequestMapping(UrlConstants.managerApi + "/core/sku")
public class GoodsSkuController extends BaseController {
    @Autowired
    private IGoodsSkuService goodsSkuService;

    /**
     * 查询物料明细列表
     */
//    @PreAuthorize("@ss.hasPermi('goods:sku:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询物料明细列表")
    public TableDataInfo list(GoodsSku goodsSku) {
        startPage();
        List<GoodsSkuQueryVO> list = goodsSkuService.selectGoodsSkuList(goodsSku);
        return getDataTable(list);
    }

    /**
     * 导出物料明细列表
     */
//    @PreAuthorize("@ss.hasPermi('goods:sku:export')")
    @Log(title = "物料明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出物料明细列表")
    public void export(HttpServletResponse response, GoodsSku goodsSku) {
        List<GoodsSkuQueryVO> list = goodsSkuService.selectGoodsSkuList(goodsSku);
        ExcelUtil<GoodsSkuQueryVO> util = new ExcelUtil<GoodsSkuQueryVO>(GoodsSkuQueryVO.class);
        util.exportExcel(response, list, "物料明细数据");
    }

    /**
     * 获取物料明细详细信息
     */
//    @PreAuthorize("@ss.hasPermi('goods:sku:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取物料明细详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long goodsId) {
        return success(goodsSkuService.selectGoodsSkuById(goodsId));
    }

    /**
     * 新增物料明细
     */
    @Transactional(rollbackFor = Exception.class)
//    @PreAuthorize("@ss.hasPermi('goods:sku:add')")
    @Log(title = "物料明细", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增物料明细")
    public AjaxResult add(@RequestBody GoodsSkuAddVO goodsSkuAddVO) {
        LoginUser loginUser = getLoginUser();
        goodsSkuAddVO.setCreateById(getUserId());
        goodsSkuAddVO.setCreateBy(getUsername());
        goodsSkuAddVO.setLoginUser(loginUser);
        return toAjax(goodsSkuService.insertGoodsSku(goodsSkuAddVO));
    }

    /**
     * 修改物料明细
     */
    @Transactional(rollbackFor = Exception.class)
//    @PreAuthorize("@ss.hasPermi('goods:sku:edit')")
    @Log(title = "物料明细", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改物料明细")
    public AjaxResult edit(@RequestBody GoodsSkuAddVO goodsSkuAddVO) {
        goodsSkuAddVO.setUpdateById(getUserId());
        goodsSkuAddVO.setUpdateBy(getUsername());
        return toAjax(goodsSkuService.updateGoodsSku(goodsSkuAddVO));
    }

    /**
     * 删除物料明细
     */
//    @PreAuthorize("@ss.hasPermi('goods:sku:remove')")
    @Log(title = "物料明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除物料明细")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(goodsSkuService.deleteGoodsSkuByIds(ids));
    }


    /**
     * 查询材料申请物料列表
     */
    @GetMapping("/purchase/list")
    @ApiOperation(value = "查询材料申请物料列表")
    public TableDataInfo purchaseList(@RequestParam("categoryId") Long categoryId, @RequestParam(value = "keywords", required = false) String keywords) {
        startPage();
        List<GoodsSkuQueryVO> list = goodsSkuService.getGoodsSkuPurchaseList(categoryId, keywords);
        return getDataTable(list);
    }

    /**
     * 零售商品列表
     */
    @PostMapping("/goodsRetail/getGoodsList")
    @ApiOperation(value = "零售商品列表")
    public TableDataInfo getGoodsListToGoodsRetail(@RequestBody RetailQueryGoodsDTO retailQueryGoodsDTO) {
        PageHelper.startPage(retailQueryGoodsDTO.getPageNum(), retailQueryGoodsDTO.getPageSize());
        List<GoodsInfo> list = goodsSkuService.getGoodsListToGoodsRetail(retailQueryGoodsDTO);
        return getDataTable(list);
    }

    /**
     * 零售商品物料列表
     */
    @GetMapping("/goodsRetail/getGoodsSkuList")
    @ApiOperation(value = "零售商品物料列表")
    public R<List<GoodsSkuQueryVO>> getGoodsSkuListToGoodsRetail(@RequestParam("id") Long goodsId) {
        List<GoodsSkuQueryVO> list = goodsSkuService.getGoodsSkuListToGoodsRetail(goodsId);
        return R.ok(list);
    }

    /**
     * 修改商品SKU状态
     */
    @Transactional(rollbackFor = Exception.class)
    @PreAuthorize("@ss.hasPermi('goods:commonGoods:edit')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PostMapping(value = "updateStatus")
    @ApiOperation(value = "修改商品SKU状态")
    public AjaxResult updateGoodsSkuStatus(@RequestBody GoodsSkuAddVO goodsSkuAddVO) {
        return toAjax(goodsSkuService.updateGoodsSkuStatus(goodsSkuAddVO));
    }

}
