package cn.ezhizao.project.business.settings.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
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 cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.project.business.settings.domain.BizSource;
import cn.ezhizao.project.business.settings.service.IBizSourceService;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.common.utils.poi.ExcelUtil;
import cn.ezhizao.framework.web.page.TableDataInfo;

/**
 * 来源Controller
 *
 * @author ruoyi
 * @date 2023-10-24
 */
@RestController
@RequestMapping("/settings/source")
public class BizSourceController extends BaseController
{
    @Resource
    private IBizSourceService bizSourceService;

    /**
     * 查询来源列表
     */
    @PreAuthorize("@ss.hasPermi('settings:source:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizSource bizSource) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizSource);
        startPage();
        startOrderBy();
        List<BizSource> list = bizSourceService.getList(bizSource);
        return getDataTable(list);
    }

    /**
     * 导出来源列表
     */
    @PreAuthorize("@ss.hasPermi('settings:source:export')")
    @Log(title = "来源", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizSource bizSource) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizSource);
        List<BizSource> list = bizSourceService.getList(bizSource);
        ExcelUtil<BizSource> util = new ExcelUtil<BizSource>(BizSource.class);
        util.exportExcel(response, list, "来源数据");
    }

    /**
     * 获取来源详细信息
     */
    @PreAuthorize("@ss.hasPermi('settings:source:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizSourceService.getById(id));
    }

    /**
     * 新增来源
     */
    @PreAuthorize("@ss.hasPermi('settings:source:add')")
    @Log(title = "来源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizSource bizSource) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizSource);
        return toAjax(bizSourceService.save(bizSource));
    }

    /**
     * 修改来源
     */
    @PreAuthorize("@ss.hasPermi('settings:source:edit')")
    @Log(title = "来源", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizSource bizSource) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizSource);
        return toAjax(bizSourceService.updateById(bizSource));
    }

    /**
     * 删除来源
     */
    @PreAuthorize("@ss.hasPermi('settings:source:remove')")
    @Log(title = "来源", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(bizSourceService.removeBatchByIds(ids));
    }
}
