package com.ruoyi.frequency.sale.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.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.frequency.sale.entity.Sale;
import com.ruoyi.frequency.sale.model.result.SaleCount;
import com.ruoyi.frequency.sale.service.SaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 销售账号Controller
 *
 * @author zj
 * @date 2023-09-22
 */
@RestController
@RequestMapping("/frequency/sale")
public class SaleController extends BaseController
{
    @Autowired
    private SaleService saleService;

    /**
     * 查询销售账号列表
     */
    @PreAuthorize("@ss.hasPermi('frequency:sale:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Sale sale)
    {
        startPage();
        List<Sale> list = saleService.selectSaleList(sale);
        return getDataTable(list);
    }

    /**
     * 统计
     */
    @PostMapping("/statistics")
    public AjaxResult statistics(@RequestBody Sale sale)
    {
        SaleCount count = saleService.statistics(sale);
        return AjaxResult.success(count);
    }

    /**
     * 导出销售账号列表
     */
    @PreAuthorize("@ss.hasPermi('frequency:sale:export')")
    @Log(title = "销售账号", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Sale sale)
    {
        List<Sale> list = saleService.selectSaleList(sale);
        ExcelUtil<Sale> util = new ExcelUtil<Sale>(Sale.class);
        return util.exportExcel(list, "销售账号数据");
    }

    /**
     * 获取销售账号详细信息
     */
    @PreAuthorize("@ss.hasPermi('frequency:sale:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(saleService.selectSaleById(id));
    }

    /**
     * 新增销售账号
     */
    @PreAuthorize("@ss.hasPermi('frequency:sale:add')")
    @Log(title = "销售账号", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Sale sale)
    {
        return toAjax(saleService.insertSale(sale));
    }

    /**
     * 修改销售账号
     */
    @PreAuthorize("@ss.hasPermi('frequency:sale:edit')")
    @Log(title = "销售账号", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Sale sale)
    {
        return toAjax(saleService.updateSale(sale));
    }

    /**
     * 删除销售账号
     */
    @PreAuthorize("@ss.hasPermi('frequency:sale:remove')")
    @Log(title = "销售账号", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}/{delFlag}")
    public AjaxResult remove(@PathVariable("ids") Long[] ids,@PathVariable("delFlag") Integer delFlag)
    {
        return toAjax(saleService.deleteSaleByIds(ids,delFlag));
    }


    /**
     * 修改余额
     */
    @PreAuthorize("@ss.hasPermi('frequency:sale:updateBalance')")
    @Log(title = "销售", businessType = BusinessType.UPDATE)
    @PostMapping("/updateBalance")
    public AjaxResult updateBalance(@RequestBody Sale sale)
    {
        return saleService.updateBalance(sale);
    }
}
