package com.ruoyi.mall.controller;

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.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mall.domain.MallSalesman;
import com.ruoyi.mall.domain.MallSalesmanAchievement;
import com.ruoyi.mall.domain.MallShopShop;
import com.ruoyi.mall.service.IMallSalesmanAchievementService;
import com.ruoyi.mall.service.IMallSalesmanService;
import com.ruoyi.mall.service.IMallShopShopService;
import org.apache.commons.lang3.ObjectUtils;
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.List;

/**
 * 业务员业绩Controller
 *
 * @author ruoyi
 * @date 2024-03-26
 */
@RestController
@RequestMapping("/mall/mallSalesmanAchievement")
public class MallSalesmanAchievementController extends BaseController {
    @Resource
    private IMallSalesmanAchievementService mallSalesmanAchievementService;
    @Resource
    private IMallShopShopService shopShopService;
    @Autowired
    private IMallSalesmanService mallSalesmanService;

    /**
     * 查询业务员业绩列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSalesmanAchievement:list')")
    @GetMapping("/list")
    public TableDataInfo list(MallSalesmanAchievement mallSalesmanAchievement) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        MallShopShop shop = shopShopService.selectMallShopShopBySysUserId(loginUser.getUserId());
        if (ObjectUtils.isNotEmpty(shop)) {
            mallSalesmanAchievement.setShopId(shop.getShopId());
        }
        startPage();
        List<MallSalesmanAchievement> list = mallSalesmanAchievementService.selectMallSalesmanAchievementList(mallSalesmanAchievement);
        return getDataTable(list);
    }
    /**
     * 查询业务员业绩列表
     */
    @PreAuthorize("@ss.hasPermi('mall:salesperformance:list')")
    @GetMapping("/salesperformancelist")
    public TableDataInfo salesperformancelist(MallSalesmanAchievement mallSalesmanAchievement) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        MallShopShop shop = shopShopService.selectMallShopShopBySysUserId(loginUser.getUserId());
        if (ObjectUtils.isNotEmpty(shop)) {
            mallSalesmanAchievement.setShopId(shop.getShopId());
        }
        MallSalesman mallSalesman = mallSalesmanService.selectMallSalesmanBysysUserId(loginUser.getUserId());
        if (ObjectUtils.isNotEmpty(mallSalesman)) {
            mallSalesmanAchievement.setSalesmanId(mallSalesman.getId().longValue());
        }
        startPage();
        List<MallSalesmanAchievement> list = mallSalesmanAchievementService.selectMallSalesmanAchievementList(mallSalesmanAchievement);
        return getDataTable(list);
    }

    /**
     * 导出业务员业绩列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSalesmanAchievement:export')")
    @Log(title = "业务员业绩", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MallSalesmanAchievement mallSalesmanAchievement) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        MallShopShop shop = shopShopService.selectMallShopShopBySysUserId(loginUser.getUserId());
        if (ObjectUtils.isNotEmpty(shop)) {
            mallSalesmanAchievement.setShopId(shop.getShopId());
        }
        List<MallSalesmanAchievement> list = mallSalesmanAchievementService.selectMallSalesmanAchievementList(mallSalesmanAchievement);
        ExcelUtil<MallSalesmanAchievement> util = new ExcelUtil<MallSalesmanAchievement>(MallSalesmanAchievement.class);
        return util.exportExcel(list, "业务员业绩数据");
    }

    /**
     * 获取业务员业绩详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSalesmanAchievement:query')")
    @GetMapping(value = "/{achievementId}")
    public AjaxResult getInfo(@PathVariable("achievementId") Long achievementId) {
        return AjaxResult.success(mallSalesmanAchievementService.selectMallSalesmanAchievementByAchievementId(achievementId));
    }

    /**
     * 新增业务员业绩
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSalesmanAchievement:add')")
    @Log(title = "业务员业绩", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallSalesmanAchievement mallSalesmanAchievement) {
        return toAjax(mallSalesmanAchievementService.insertMallSalesmanAchievement(mallSalesmanAchievement));
    }

    /**
     * 修改业务员业绩
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSalesmanAchievement:edit')")
    @Log(title = "业务员业绩", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallSalesmanAchievement mallSalesmanAchievement) {
        return toAjax(mallSalesmanAchievementService.updateMallSalesmanAchievement(mallSalesmanAchievement));
    }

    /**
     * 删除业务员业绩
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSalesmanAchievement:remove')")
    @Log(title = "业务员业绩", businessType = BusinessType.DELETE)
    @DeleteMapping("/{achievementIds}")
    public AjaxResult remove(@PathVariable Long[] achievementIds) {
        return toAjax(mallSalesmanAchievementService.deleteMallSalesmanAchievementByAchievementIds(achievementIds));
    }
}
