package com.ruoyi.mall.controller;

import java.util.List;
import java.util.Map;

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.MallShopSupplier;
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.MallShopShop;
import com.ruoyi.mall.service.IMallShopShopService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 商家管理Controller
 *
 * @author ruoyi
 * @date 2024-03-28
 */
@RestController
@RequestMapping("/mall/mallShopShop")
public class MallShopShopController extends BaseController
{
    @Resource
    private IMallShopShopService mallShopShopService;
    @Resource
    private ISysUserService userService;

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

    /**
     * 导出商家管理列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallShopShop:export')")
    @Log(title = "商家管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MallShopShop mallShopShop)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        MallShopShop shop = mallShopShopService.selectMallShopShopBySysUserId(loginUser.getUserId());
        if (ObjectUtils.isNotEmpty(shop)){
            mallShopShop.setShopId(shop.getShopId());
        }
        List<MallShopShop> list = mallShopShopService.selectMallShopShopList(mallShopShop);
        ExcelUtil<MallShopShop> util = new ExcelUtil<MallShopShop>(MallShopShop.class);
        return util.exportExcel(list, "商家管理数据");
    }

    /**
     * 获取商家管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:mallShopShop:query')")
    @GetMapping(value = "/{shopId}")
    public AjaxResult getInfo(@PathVariable("shopId") Long shopId)
    {
        return AjaxResult.success(mallShopShopService.selectMallShopShopByShopId(shopId));
    }

    /**
     * 新增商家管理
     */
    @PreAuthorize("@ss.hasPermi('mall:mallShopShop:add')")
    @Log(title = "商家管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallShopShop mallShopShop)
    {
        MallShopShop shopByShopName = mallShopShopService.selectMallShopShopByShopName(mallShopShop.getShopName());
        if (shopByShopName != null) {
            return AjaxResult.error("商家名称已存在");
        }
        MallShopShop shopByShopAccount = mallShopShopService.selectMallShopShopByShopAccount(mallShopShop.getShopAccount());
        if (shopByShopAccount != null) {
            return AjaxResult.error("商家账号已存在");
        }

        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(mallShopShop.getShopAccount())))
        {
            return AjaxResult.error("新增商家'" + mallShopShop.getShopAccount() + "'失败，登录账号已存在");
        }
        return toAjax(mallShopShopService.insertMallShopShop(mallShopShop));
    }

    /**
     * 修改商家管理
     */
    @PreAuthorize("@ss.hasPermi('mall:mallShopShop:edit')")
    @Log(title = "商家管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallShopShop mallShopShop)
    {
        MallShopShop shopByShopName = mallShopShopService.selectMallShopShopByShopName(mallShopShop.getShopName());
        if (shopByShopName != null && !shopByShopName.getShopId().equals(mallShopShop.getShopId())) {
            return AjaxResult.error("商家名称已存在");
        }
        MallShopShop shopByShopAccount = mallShopShopService.selectMallShopShopByShopAccount(mallShopShop.getShopAccount());
        if (shopByShopAccount != null && !shopByShopAccount.getShopId().equals(mallShopShop.getShopId())) {
            return AjaxResult.error("商家账号已存在");
        }

        MallShopShop shopBySysUserId = mallShopShopService.selectMallShopShopBySysUserId(mallShopShop.getSysUserId());
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(mallShopShop.getShopAccount())) && !shopBySysUserId.getSysUserId().equals(mallShopShop.getSysUserId()))
        {
            return AjaxResult.error("修改商家'" + mallShopShop.getShopAccount() + "'失败，登录账号已存在");
        }
        return toAjax(mallShopShopService.updateMallShopShop(mallShopShop));
    }

    /**
     * 删除商家管理
     */
    @PreAuthorize("@ss.hasPermi('mall:mallShopShop:remove')")
    @Log(title = "商家管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{shopIds}")
    public AjaxResult remove(@PathVariable Long[] shopIds)
    {
        return toAjax(mallShopShopService.deleteMallShopShopByShopIds(shopIds));
    }

    /**
     * 后台重置商家密码
     *
     * @param param
     * @return
     */
    @PreAuthorize("@ss.hasPermi('mall:mallShopShop:edit')")
    @PostMapping("/resetPasswordShop")
    public AjaxResult resetPasswordShop(@RequestBody Map<String,Object> param){
        if (!param.containsKey("shopId") || !param.containsKey("newPassword")){
            return AjaxResult.error("缺少参数");
        }
        try {
            return mallShopShopService.resetPassword(param);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("网络异常");
        }
    }

    /**
     * 店铺和供应商级联列表
     * @return
     */
    @GetMapping("/selectAllShopList")
    public AjaxResult selectAllShopList(){
        return AjaxResult.success(mallShopShopService.selectAllShopList());
    }

    @GetMapping("/selectShopShopList")
    public AjaxResult selectShopShopList(MallShopShop mallShopShop)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        MallShopShop shop = mallShopShopService.selectMallShopShopBySysUserId(loginUser.getUserId());
        if (ObjectUtils.isNotEmpty(shop)){
            mallShopShop.setShopId(shop.getShopId());
        }
        mallShopShop.setDelFlag(0);
        startPage();
        List<MallShopShop> list = mallShopShopService.selectMallShopShopList(mallShopShop);
        return AjaxResult.success(list);
    }
}
