package com.xinelu.manage.controller.goodsInfo;

import com.xinelu.common.annotation.Log;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.custominterface.Insert;
import com.xinelu.common.custominterface.Update;
import com.xinelu.common.enums.BusinessType;
import com.xinelu.common.utils.poi.ExcelUtil;
import com.xinelu.manage.domain.goodsInfo.GoodsInfo;
import com.xinelu.manage.dto.goodsInfo.GoodsInfoDTO;
import com.xinelu.manage.service.goodsInfo.IGoodsInfoService;
import com.xinelu.manage.vo.goodsInfo.GetGoodDetails;
import com.xinelu.manage.vo.goodsInfo.GoodsInfoVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 护理站商品基本信息Controller
 *
 * @author xinyilu
 * @date 2022-10-17
 */
@RestController
@RequestMapping("/system/goodsInfo")
public class GoodsInfoController extends BaseController {
    @Resource
    private IGoodsInfoService goodsInfoService;

    /**
     * 查询商品基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:goodsInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsInfo goodsInfo) {
        startPage();
        List<GoodsInfo> list = goodsInfoService.selectGoodsInfoList(goodsInfo);
        return getDataTable(list);
    }

    /**
     * 导出商品基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:goodsInfo:export')")
    @Log(title = "商品基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsInfo goodsInfo) {
        List<GoodsInfo> list = goodsInfoService.selectGoodsInfoList(goodsInfo);
        ExcelUtil<GoodsInfo> util = new ExcelUtil<>(GoodsInfo.class);
        util.exportExcel(response, list, "商品基本信息数据");
    }

    /**
     * 获取商品基本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:goodsInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(goodsInfoService.selectGoodsInfoById(id));
    }

    /**
     * 新增商品基本信息
     */
    @PreAuthorize("@ss.hasPermi('system:goodsInfo:add')")
    @Log(title = "商品基本信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated(Insert.class) @RequestBody GoodsInfoDTO goodsInfo) {
        return goodsInfoService.insertGoodsInfo(goodsInfo);
    }

    /**
     * 修改商品基本信息
     */
    @PreAuthorize("@ss.hasPermi('system:goodsInfo:edit')")
    @Log(title = "商品基本信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated(Update.class) @RequestBody GoodsInfoDTO goodsInfo) {
        if (Objects.isNull(goodsInfo) || Objects.isNull(goodsInfo.getId())) {
            return AjaxResult.error("商品信息为空,请联系管理员！");
        }
        return goodsInfoService.updateGoodsInfo(goodsInfo);
    }

    /**
     * 删除商品基本信息
     */
    @PreAuthorize("@ss.hasPermi('system:goodsInfo:remove')")
    @Log(title = "商品基本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return goodsInfoService.deleteGoodsInfoByIds(ids);
    }

    /**
     * 查询商品基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:goodsInfo:goodsInfoList')")
    @GetMapping("/goodsInfoList")
    public TableDataInfo goodsInfoList(GoodsInfoVO goodsInfo) {
        startPage();
        List<GoodsInfoVO> goodsInfoList = goodsInfoService.getGoodsInfoList(goodsInfo);
        return getDataTable(goodsInfoList);
    }

    /**
     * 根据商品id查询商品详细信息
     *
     * @param goodsInfoId 商品详细id
     * @return com.xinyilu.nurseapplet.domain.nearbynursingstation.NearbyNursingStationVO
     **/
    @PreAuthorize("@ss.hasPermi('system:goodsInfo:goodsDetails')")
    @GetMapping("/goodsDetails")
    public AjaxResult goodsDetails(Long goodsInfoId) {
        if (Objects.isNull(goodsInfoId)) {
            return AjaxResult.error("商品详细id不能为空！");
        }
        GetGoodDetails goodsDetails = goodsInfoService.getGoodsDetails(goodsInfoId);
        return AjaxResult.success(goodsDetails);
    }

    /**
     * 修改商品上架状态基本信息
     */
    @PreAuthorize("@ss.hasPermi('system:goodsInfo:editGoodsWhetherShelf')")
    @Log(title = "商品基本信息", businessType = BusinessType.UPDATE)
    @PostMapping("/editGoodsWhetherShelf")
    public AjaxResult editGoodsWhetherShelf(@RequestBody GoodsInfo goodsInfo) {
        return AjaxResult.success(goodsInfoService.updateGoodsWhetherShelf(goodsInfo));
    }
}
