package com.ruoyi.mall.controller;

import com.alibaba.fastjson.JSON;
import com.google.gson.JsonObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.common.utils.JPushUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mall.domain.*;
import com.ruoyi.mall.domain.vo.MallGoodsGoodsExportVO;
import com.ruoyi.mall.domain.vo.MallGoodsGoodsListVo;
import com.ruoyi.mall.mapper.MallShopShopMapper;
import com.ruoyi.mall.mapper.MallShopSupplierMapper;
import com.ruoyi.mall.service.IMallGoodsGoodsService;
import com.ruoyi.mall.service.IMallMemberUserService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 商品Controller
 *
 * @author chang
 * @date 2021-11-25
 */
@RestController
@RequestMapping("/mall/mallGoodsGoods")
public class MallGoodsGoodsController extends BaseController {
    @Autowired
    private IMallGoodsGoodsService mallGoodsGoodsService;
    @Resource
    private MallShopShopMapper mallShopShopMapper;
    @Resource
    private MallShopSupplierMapper mallShopSupplierMapper;
    @Resource
    private IMallMemberUserService memberUserService;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private JPushUtils jPushUtils;

    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallGoodsGoods:list')")
    @GetMapping("/list")
    public TableDataInfo list(MallGoodsGoodsListVo mallGoodsGoods) {
        // 获取登陆用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        MallShopShop mallShopShop = mallShopShopMapper.selectMallShopShopBySysUserId(loginUser.getUserId());
        if (mallShopShop != null && loginUser.getUserId() != 1) {
            mallGoodsGoods.setShopId(mallShopShop.getShopId());
        }
        MallShopSupplier mallShopSupplier = mallShopSupplierMapper.selectMallShopSupplierBySysUserId(loginUser.getUserId());
        if (mallShopSupplier != null && loginUser.getUserId() != 1) {
            mallGoodsGoods.setSupplierId(mallShopSupplier.getSupplierId());
        }
        startPage();
        MallGoodsGoods mallGoodsGoods1 = new MallGoodsGoods();
        BeanUtils.copyProperties(mallGoodsGoods, mallGoodsGoods1);
        List<MallGoodsGoods> list = mallGoodsGoodsService.selectMallGoodsGoodsList(mallGoodsGoods1);
        return getDataTable(list);
    }

    /**
     * 查询其他商铺/供应商商品列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallGoodsGoods:list')")
    @GetMapping("/otherList")
    public TableDataInfo otherList(MallGoodsGoods mallGoodsGoods) {
        // 获取登陆用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        MallShopShop mallShopShop = mallShopShopMapper.selectMallShopShopBySysUserId(loginUser.getUserId());
        if (mallShopShop != null && loginUser.getUserId() != 1) {
            mallGoodsGoods.setShopId(mallShopShop.getShopId());
        }

        MallShopSupplier mallShopSupplier = mallShopSupplierMapper.selectMallShopSupplierBySysUserId(loginUser.getUserId());
        if (mallShopSupplier != null && loginUser.getUserId() != 1) {
            mallGoodsGoods.setSupplierId(mallShopSupplier.getSupplierId());
        }

        startPage();
        mallGoodsGoods.setCheckStatus(1);
        List<MallGoodsGoods> list = mallGoodsGoodsService.selectOtherGoodsList(mallGoodsGoods);
        return getDataTable(list);
    }

    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallGoodsGoods:list')")
    @GetMapping("/goodsGoodsSelectList")
    public AjaxResult goodsGoodsSelectList() {
        return AjaxResult.success(mallGoodsGoodsService.goodsGoodsSelectList());
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallGoodsGoods:export')")
    @Log(title = "导出筛选商品列表", businessType = BusinessType.EXPORT)
    @GetMapping("/exportScreen")
    public AjaxResult exportScreen(MallGoodsGoods mallGoodsGoods) {
        // 获取登陆用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        MallShopShop mallShopShop = mallShopShopMapper.selectMallShopShopBySysUserId(loginUser.getUserId());
        if (mallShopShop != null && loginUser.getUserId() != 1) {
            mallGoodsGoods.setShopId(mallShopShop.getShopId());
        }

        MallShopSupplier mallShopSupplier = mallShopSupplierMapper.selectMallShopSupplierBySysUserId(loginUser.getUserId());
        if (mallShopSupplier != null && loginUser.getUserId() != 1) {
            mallGoodsGoods.setSupplierId(mallShopSupplier.getSupplierId());
        }

        List<MallGoodsGoodsExportVO> list = mallGoodsGoodsService.selectExportScreenGoodsList(mallGoodsGoods);
        ExcelUtil<MallGoodsGoodsExportVO> util = new ExcelUtil<MallGoodsGoodsExportVO>(MallGoodsGoodsExportVO.class);
        return util.exportExcel(list, "商品数据");
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallGoodsGoods:export')")
    @Log(title = "导出勾选商品列表", businessType = BusinessType.EXPORT)
    @PostMapping("/exportCheck")
    public AjaxResult exportCheck(@RequestBody Long[] goodsIds) {
        List<MallGoodsGoodsExportVO> list = mallGoodsGoodsService.selectExportCheckGoodsList(goodsIds);
        ExcelUtil<MallGoodsGoodsExportVO> util = new ExcelUtil<MallGoodsGoodsExportVO>(MallGoodsGoodsExportVO.class);
        return util.exportExcel(list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:mallGoodsGoods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(mallGoodsGoodsService.selectMallGoodsGoodsById(id));
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('mall:mallGoodsGoods:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallGoodsGoods mallGoodsGoods) {
        if (StringUtils.isEmpty(mallGoodsGoods.getGoodsProductList()) || mallGoodsGoods.getGoodsProductList().size() < 1) {
            return AjaxResult.error("请新增规格");
        }
        try {
            return toAjax(mallGoodsGoodsService.insertMallGoodsGoods(mallGoodsGoods));
        } catch (Exception e) {
            return AjaxResult.error("填写内容不完整，请仔细填写");
        }
    }

    /**
     * 修改商品
     */
    // @PreAuthorize("@ss.hasPermi('mall:mallGoodsGoods:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallGoodsGoods mallGoodsGoods) {
        if (StringUtils.isEmpty(mallGoodsGoods.getGoodsProductList()) || mallGoodsGoods.getGoodsProductList().size() < 1) {
            return AjaxResult.error("请新增规格");
        }
        return toAjax(mallGoodsGoodsService.updateMallGoodsGoods(mallGoodsGoods));
    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('mall:mallGoodsGoods:remove')")
    @Log(title = "删除商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        mallGoodsGoodsService.deleteCopyMallGoodsGoodsByCopyIds(ids);
        return toAjax(mallGoodsGoodsService.deleteMallGoodsGoodsByIds(ids));
    }

    /**
     * 修改审核状态
     *
     * @param mallGoodsGoods
     * @return
     */
    @PreAuthorize("@ss.hasPermi('mall:mallGoodsGoods:updateCheckStatus')")
    @PostMapping("/updateCheckStatus")
    public AjaxResult updateCheckStatus(@RequestBody MallGoodsGoods mallGoodsGoods) {
        return mallGoodsGoodsService.updateCheckStatus(mallGoodsGoods);
    }

    /**
     * 修改供应商
     *
     * @param mallGoodsGoods
     * @return
     */
    @PreAuthorize("@ss.hasPermi('mall:mallGoodsGoods:updateSupplier')")
    @PostMapping("/updateSupplier")
    public AjaxResult updateSupplier(@RequestBody MallGoodsGoods mallGoodsGoods) {
        return mallGoodsGoodsService.updateSupplier(mallGoodsGoods);
    }

    @PostMapping("/Goodssort")
    public AjaxResult Goodssort() {
        return mallGoodsGoodsService.selectgoodssort();
    }


    @PostMapping("/allGoods")
    public AjaxResult allGoods() {
        return mallGoodsGoodsService.selectallGoods();
    }


}
