package com.fs.company.controller;

import java.util.List;

import cn.hutool.core.util.IdUtil;
import com.fs.common.core.domain.R;
import com.fs.common.utils.ServletUtils;
import com.fs.company.domain.Company;
import com.fs.company.domain.CompanyRecharge;
import com.fs.core.security.LoginUser;
import com.fs.core.web.service.TokenService;
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.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.company.domain.CompanyTcmSchedule;
import com.fs.company.service.ICompanyTcmScheduleService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 中医档期管理Controller
 *
 * @author fs
 * @date 2023-09-05
 */
@RestController
@RequestMapping("/company/schedule")
public class CompanyTcmScheduleController extends BaseController
{
    @Autowired
    private ICompanyTcmScheduleService companyTcmScheduleService;

    @Autowired
    private TokenService tokenService;
    /**
     * 查询中医档期管理列表
     */
    @PreAuthorize("@ss.hasPermi('company:schedule:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyTcmSchedule companyTcmSchedule)
    {
        startPage();
        List<CompanyTcmSchedule> list = companyTcmScheduleService.selectCompanyTcmScheduleList(companyTcmSchedule);
        return getDataTable(list);
    }

    @GetMapping("/getTcmScheduleList")
    public R getCompanyTcmScheduleList()
    {
        CompanyTcmSchedule map = new CompanyTcmSchedule();
        map.setStatus(1L);
        List<CompanyTcmSchedule> list = companyTcmScheduleService.selectCompanyTcmScheduleList(map);
        return R.ok().put("data",list);
    }

    /**
     * 导出中医档期管理列表
     */
    @PreAuthorize("@ss.hasPermi('company:schedule:export')")
    @Log(title = "中医档期管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyTcmSchedule companyTcmSchedule)
    {
        List<CompanyTcmSchedule> list = companyTcmScheduleService.selectCompanyTcmScheduleList(companyTcmSchedule);
        ExcelUtil<CompanyTcmSchedule> util = new ExcelUtil<CompanyTcmSchedule>(CompanyTcmSchedule.class);
        return util.exportExcel(list, "schedule");
    }

    /**
     * 获取中医档期管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:schedule:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(companyTcmScheduleService.selectCompanyTcmScheduleById(id));
    }

    /**
     * 新增中医档期管理
     */
    @PreAuthorize("@ss.hasPermi('company:schedule:add')")
    @Log(title = "中医档期管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompanyTcmSchedule companyTcmSchedule)
    {

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        List<CompanyTcmSchedule> scheduleList=companyTcmScheduleService.selectCompanyTcmScheduleList(companyTcmSchedule);
        if(scheduleList.size()>0){
            return AjaxResult.error("档期名称已经存在");
        }
        companyTcmSchedule.setCreateUserId(loginUser.getUser().getUserId());
        //companyTcmSchedule.setUpdateUserId(loginUser.getUser().getUserId());
        return toAjax(companyTcmScheduleService.insertCompanyTcmSchedule(companyTcmSchedule));
    }

    /**
     * 修改中医档期管理
     */
    @PreAuthorize("@ss.hasPermi('company:schedule:edit')")
    @Log(title = "中医档期管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CompanyTcmSchedule companyTcmSchedule)
    {
        return toAjax(companyTcmScheduleService.updateCompanyTcmSchedule(companyTcmSchedule));
    }

    /**
     * 删除中医档期管理
     */
    @PreAuthorize("@ss.hasPermi('company:schedule:remove')")
    @Log(title = "中医档期管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(companyTcmScheduleService.deleteCompanyTcmScheduleByIds(ids));
    }
}
