package com.fs.company.controller.company;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.domain.CompanyTcmConsume;
import com.fs.company.service.ICompanyTcmConsumeService;
import com.fs.company.service.ICompanyTcmScheduleService;
import com.fs.company.vo.CompanyTcmConsumeVO;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
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 fs
 * @date 2023-09-15
 */
@RestController
@RequestMapping("/company/consume")
public class CompanyTcmConsumeController extends BaseController
{
    @Autowired
    private ICompanyTcmConsumeService companyTcmConsumeService;

    @Autowired
    private ICompanyTcmScheduleService companyTcmScheduleService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询公司档期消费列表
     */
    @PreAuthorize("@ss.hasPermi('company:consume:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyTcmConsume companyTcmConsume)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        companyTcmConsume.setCompanyId(loginUser.getCompany().getCompanyId());
        List<CompanyTcmConsumeVO> list = companyTcmConsumeService.selectCompanyTcmConsumeListVO(companyTcmConsume);
        return getDataTable(list);
    }

    /**
     * 导出公司档期消费列表
     */
    @PreAuthorize("@ss.hasPermi('company:consume:export')")
    @Log(title = "公司档期消费", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyTcmConsume companyTcmConsume)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        companyTcmConsume.setCompanyId(loginUser.getCompany().getCompanyId());
        List<CompanyTcmConsumeVO> list = companyTcmConsumeService.selectCompanyTcmConsumeListVO(companyTcmConsume);
        ExcelUtil<CompanyTcmConsumeVO> util = new ExcelUtil<CompanyTcmConsumeVO>(CompanyTcmConsumeVO.class);
        return util.exportExcel(list, "consume");
    }

    /**
     * 获取公司档期消费详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:consume:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(companyTcmConsumeService.selectCompanyTcmConsumeById(id));
    }

    /**
     * 新增公司档期消费
     */
    @PreAuthorize("@ss.hasPermi('company:consume:add')")
    @Log(title = "公司档期消费", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompanyTcmConsume companyTcmConsume)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        companyTcmConsume.setCreateUserId(loginUser.getUser().getUserId());
        companyTcmConsume.setCompanyId(loginUser.getCompany().getCompanyId());
        if(companyTcmConsumeService.selectCompanyTcmConsumeBySchedule(companyTcmConsume.getCompanyId(),companyTcmConsume.getScheduleId())!=null){
             return  AjaxResult.error("档期消费数据已存在");
        }
        return toAjax(companyTcmConsumeService.insertCompanyTcmConsume(companyTcmConsume));
    }

    /**
     * 修改公司档期消费
     */
    @PreAuthorize("@ss.hasPermi('company:consume:edit')")
    @Log(title = "公司档期消费", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CompanyTcmConsume companyTcmConsume)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        companyTcmConsume.setCompanyId(loginUser.getCompany().getCompanyId());
        return toAjax(companyTcmConsumeService.updateCompanyTcmConsume(companyTcmConsume));
    }

    /**
     * 删除公司档期消费
     */
    @PreAuthorize("@ss.hasPermi('company:consume:remove')")
    @Log(title = "公司档期消费", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(companyTcmConsumeService.deleteCompanyTcmConsumeByIds(ids));
    }
}
