package com.ruoyi.mall.controller;

import java.util.List;

import com.ruoyi.mall.domain.MallSalesman;
import com.ruoyi.mall.service.IMallSalesmanService;
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.MallAppCompany;
import com.ruoyi.mall.service.IMallAppCompanyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * app-公司Controller
 *
 * @author ruoyi
 * @date 2024-06-27
 */
@RestController
@RequestMapping("/mall/mallAppCompany")
public class MallAppCompanyController extends BaseController
{
    @Resource
    private IMallAppCompanyService mallAppCompanyService;
    @Resource
    private IMallSalesmanService mallSalesmanService;

    /**
     * 查询app-公司列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallAppCompany:list')")
    @GetMapping("/list")
    public TableDataInfo list(MallAppCompany mallAppCompany)
    {
        startPage();
        mallAppCompany.setDelFlag(0);
        List<MallAppCompany> list = mallAppCompanyService.selectMallAppCompanyList(mallAppCompany);
        return getDataTable(list);
    }

    /**
     * 导出app-公司列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallAppCompany:export')")
    @Log(title = "app-公司", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MallAppCompany mallAppCompany)
    {
        List<MallAppCompany> list = mallAppCompanyService.selectMallAppCompanyList(mallAppCompany);
        ExcelUtil<MallAppCompany> util = new ExcelUtil<MallAppCompany>(MallAppCompany.class);
        return util.exportExcel(list, "app-公司数据");
    }

    /**
     * 获取app-公司详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:mallAppCompany:query')")
    @GetMapping(value = "/{companyId}")
    public AjaxResult getInfo(@PathVariable("companyId") Long companyId)
    {
        return AjaxResult.success(mallAppCompanyService.selectMallAppCompanyByCompanyId(companyId));
    }

    /**
     * 新增app-公司
     */
    @PreAuthorize("@ss.hasPermi('mall:mallAppCompany:add')")
    @Log(title = "app-公司", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallAppCompany mallAppCompany)
    {
        return toAjax(mallAppCompanyService.insertMallAppCompany(mallAppCompany));
    }

    /**
     * 修改app-公司
     */
    @PreAuthorize("@ss.hasPermi('mall:mallAppCompany:edit')")
    @Log(title = "app-公司", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallAppCompany mallAppCompany)
    {
        return toAjax(mallAppCompanyService.updateMallAppCompany(mallAppCompany));
    }

    /**
     * 删除app-公司
     */
    @PreAuthorize("@ss.hasPermi('mall:mallAppCompany:remove')")
    @Log(title = "app-公司", businessType = BusinessType.DELETE)
	@DeleteMapping("/{companyIds}")
    public AjaxResult remove(@PathVariable Long[] companyIds)
    {
        for (Long companyId : companyIds) {
            MallSalesman mallSalesman = new MallSalesman();
            mallSalesman.setCompanyId(companyId);
            mallSalesman.setDelFlag(0);
            List<MallSalesman> salesmanList = mallSalesmanService.selectMallSalesmanList(mallSalesman);
            if (salesmanList.size() > 0) {
                return AjaxResult.error("公司人员存在，不可删除公司");
            }
        }
        return toAjax(mallAppCompanyService.deleteMallAppCompanyByCompanyIds(companyIds));
    }
}
