package com.ruoyi.mall.controller;

import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mall.domain.MallShopShop;
import com.ruoyi.mall.service.IMallShopShopService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.lang3.ObjectUtils;
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.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mall.domain.MallShopSupplier;
import com.ruoyi.mall.service.IMallShopSupplierService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 商家供应商管理Controller
 *
 * @author ruoyi
 * @date 2024-04-19
 */
@RestController
@RequestMapping("/mall/mallShopSupplier")
public class MallShopSupplierController extends BaseController
{
    @Resource
    private IMallShopSupplierService mallShopSupplierService;
    @Resource
    private ISysUserService userService;
    @Resource
    private IMallShopShopService mallShopShopService;

    /**
     * 查询商家供应商管理列表
     */
    //@PreAuthorize("@ss.hasPermi('mall:mallShopSupplier:list')")
    @GetMapping("/list")
    public TableDataInfo list(MallShopSupplier mallShopSupplier)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        MallShopShop shop = mallShopShopService.selectMallShopShopBySysUserId(loginUser.getUserId());
        if (ObjectUtils.isNotEmpty(shop)){
            mallShopSupplier.setShopId(shop.getShopId());
        }
        startPage();
        List<MallShopSupplier> list = mallShopSupplierService.selectMallShopSupplierList(mallShopSupplier);
        return getDataTable(list);
    }

    /**
     * 导出商家供应商管理列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallShopSupplier:export')")
    @Log(title = "商家供应商管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MallShopSupplier mallShopSupplier)
    {
        List<MallShopSupplier> list = mallShopSupplierService.selectMallShopSupplierList(mallShopSupplier);
        ExcelUtil<MallShopSupplier> util = new ExcelUtil<MallShopSupplier>(MallShopSupplier.class);
        return util.exportExcel(list, "商家供应商管理数据");
    }

    /**
     * 获取商家供应商管理详细信息
     */
    //@PreAuthorize("@ss.hasPermi('mall:mallShopSupplier:query')")
    @GetMapping(value = "/{supplierId}")
    public AjaxResult getInfo(@PathVariable("supplierId") Long supplierId)
    {
        return AjaxResult.success(mallShopSupplierService.selectMallShopSupplierBySupplierId(supplierId));
    }

    /**
     * 新增商家供应商管理
     */
    //@PreAuthorize("@ss.hasPermi('mall:mallShopSupplier:add')")
    @Log(title = "商家供应商管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallShopSupplier mallShopSupplier)
    {
        MallShopSupplier supplierBySupplierName = mallShopSupplierService.selectMallShopSupplierBySupplierName(mallShopSupplier.getSupplierName());
        if (supplierBySupplierName != null) {
            return AjaxResult.error("供应商名称已存在");
        }

        MallShopSupplier supplierBySupplierAccount = mallShopSupplierService.selectMallShopSupplierBySupplierAccount(mallShopSupplier.getSupplierAccount());
        if (supplierBySupplierAccount != null) {
            return AjaxResult.error("供应商账号已存在");
        }

        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(mallShopSupplier.getSupplierAccount())))
        {
            return AjaxResult.error("新增供应商'" + mallShopSupplier.getSupplierAccount() + "'失败，登录账号已存在");
        }
        return toAjax(mallShopSupplierService.insertMallShopSupplier(mallShopSupplier));
    }

    /**
     * 修改商家供应商管理
     */
    //@PreAuthorize("@ss.hasPermi('mall:mallShopSupplier:edit')")
    @Log(title = "商家供应商管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallShopSupplier mallShopSupplier)
    {
        MallShopSupplier supplierBySupplierName = mallShopSupplierService.selectMallShopSupplierBySupplierName(mallShopSupplier.getSupplierName());
        if (supplierBySupplierName != null && !supplierBySupplierName.getSupplierId().equals(mallShopSupplier.getSupplierId())) {
            return AjaxResult.error("供应商名称已存在");
        }

        MallShopSupplier supplierBySupplierAccount = mallShopSupplierService.selectMallShopSupplierBySupplierAccount(mallShopSupplier.getSupplierAccount());
        if (supplierBySupplierAccount != null && !supplierBySupplierAccount.getSupplierId().equals(mallShopSupplier.getSupplierId())) {
            return AjaxResult.error("供应商账号已存在");
        }

        MallShopSupplier supplierBySysUserId = mallShopSupplierService.selectMallShopSupplierBySysUserId(mallShopSupplier.getSysUserId());
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(mallShopSupplier.getSupplierAccount())) && !supplierBySysUserId.getSysUserId().equals(mallShopSupplier.getSysUserId()))
        {
            return AjaxResult.error("修改供应商'" + mallShopSupplier.getSupplierAccount() + "'失败，登录账号已存在");
        }
        return toAjax(mallShopSupplierService.updateMallShopSupplier(mallShopSupplier));
    }

    /**
     * 删除商家供应商管理
     */
    //@PreAuthorize("@ss.hasPermi('mall:mallShopSupplier:remove')")
    @Log(title = "商家供应商管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{supplierIds}")
    public AjaxResult remove(@PathVariable Long[] supplierIds)
    {
        if (supplierIds.length > 0) {
            for (Long supplierId : supplierIds) {
                MallShopSupplier mallShopSupplier = mallShopSupplierService.selectMallShopSupplierBySupplierId(supplierId);
                if (mallShopSupplier != null) {
                    userService.deleteReallyUserById(mallShopSupplier.getSysUserId());
                }
            }
        }
        return toAjax(mallShopSupplierService.deleteMallShopSupplierBySupplierIds(supplierIds));
    }
}
