package com.fs.company.controller.course;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.fs.company.service.impl.CompanyDeptServiceImpl;
import com.fs.company.service.impl.CompanyUserServiceImpl;
import com.fs.framework.security.LoginUser;
import com.fs.framework.security.SecurityUtils;
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.course.domain.FsCourseFinishTempParent;
import com.fs.course.service.IFsCourseFinishTempParentService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 完课模板Controller
 *
 * @author 吴树波
 * @date 2025-05-22
 */
@RestController
@RequestMapping("/course/courseFinishTempParent")
public class FsCourseFinishTempParentController extends BaseController
{
    @Autowired
    private IFsCourseFinishTempParentService fsCourseFinishTempParentService;

    @Autowired
    private CompanyDeptServiceImpl companyDeptService;

    @Autowired
    private CompanyUserServiceImpl companyUserService;

    /**
     * 查询完课模板列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseFinishTempParent:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsCourseFinishTempParent fsCourseFinishTempParent)
    {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fsCourseFinishTempParent.setCompanyId(loginUser.getCompany().getCompanyId());
        List<FsCourseFinishTempParent> list = fsCourseFinishTempParentService.selectFsCourseFinishTempParentList(fsCourseFinishTempParent);
        return getDataTable(list);
    }

    /**
     * 查询我创建的完课模板列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseFinishTempParent:myList')")
    @GetMapping("/myList")
    public TableDataInfo myList(FsCourseFinishTempParent fsCourseFinishTempParent)
    {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fsCourseFinishTempParent.setCompanyId(loginUser.getCompany().getCompanyId());
        fsCourseFinishTempParent.setCreateBy(String.valueOf(loginUser.getUser().getUserId()));
        List<FsCourseFinishTempParent> list = fsCourseFinishTempParentService.selectFsCourseFinishTempParentList(fsCourseFinishTempParent);
        return getDataTable(list);
    }

    /**
     * 查询部门的创建的完课模板列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseFinishTempParent:deptList')")
    @GetMapping("/deptList")
    public TableDataInfo deptList(FsCourseFinishTempParent fsCourseFinishTempParent)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fsCourseFinishTempParent.setCompanyId(loginUser.getCompany().getCompanyId());

        List<Long> combinedList = new ArrayList<>();
        //本部门
        Long deptId = loginUser.getUser().getDeptId();
        if (deptId!=null){
            combinedList.add(deptId);
        }
        //本部门的下级部门
        List<Long> deptList = companyDeptService.selectCompanyDeptByParentId(deptId);
        if (!deptList.isEmpty()){
            combinedList.addAll(deptList);
        }

        List<Long> userIds = companyUserService.selectCompanyQwUserByDept(combinedList, loginUser.getUser().getUserType());
        if (userIds.isEmpty()){
            return getDataTable(new ArrayList<>());
        }

        fsCourseFinishTempParent.setUserIds(userIds);

        startPage();
        List<FsCourseFinishTempParent> list = fsCourseFinishTempParentService.selectFsCourseFinishTempParentList(fsCourseFinishTempParent);
        return getDataTable(list);
    }

    /**
     * 导出完课模板列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseFinishTempParent:export')")
    @Log(title = "完课模板", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsCourseFinishTempParent fsCourseFinishTempParent)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fsCourseFinishTempParent.setCompanyId(loginUser.getCompany().getCompanyId());
        List<FsCourseFinishTempParent> list = fsCourseFinishTempParentService.selectFsCourseFinishTempParentList(fsCourseFinishTempParent);
        ExcelUtil<FsCourseFinishTempParent> util = new ExcelUtil<FsCourseFinishTempParent>(FsCourseFinishTempParent.class);
        return util.exportExcel(list, "完课模板数据");
    }

    /**
     * 导出我的完课模板列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseFinishTempParent:myExport')")
    @Log(title = "完课模板", businessType = BusinessType.EXPORT)
    @GetMapping("/myExport")
    public AjaxResult myExport(FsCourseFinishTempParent fsCourseFinishTempParent)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fsCourseFinishTempParent.setCompanyId(loginUser.getCompany().getCompanyId());
        fsCourseFinishTempParent.setCreateBy(String.valueOf(loginUser.getUser().getUserId()));
        List<FsCourseFinishTempParent> list = fsCourseFinishTempParentService.selectFsCourseFinishTempParentList(fsCourseFinishTempParent);
        ExcelUtil<FsCourseFinishTempParent> util = new ExcelUtil<FsCourseFinishTempParent>(FsCourseFinishTempParent.class);
        return util.exportExcel(list, "完课模板数据");
    }

    /**
     * 导出部门完课模板列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseFinishTempParent:myExport')")
    @Log(title = "完课模板", businessType = BusinessType.EXPORT)
    @GetMapping("/deptExport")
    public AjaxResult deptExport(FsCourseFinishTempParent fsCourseFinishTempParent)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        List<Long> combinedList = new ArrayList<>();
        //本部门
        Long deptId = loginUser.getUser().getDeptId();
        if (deptId!=null){
            combinedList.add(deptId);
        }
        //本部门的下级部门
        List<Long> deptList = companyDeptService.selectCompanyDeptByParentId(deptId);
        if (!deptList.isEmpty()){
            combinedList.addAll(deptList);
        }

        List<Long> userIds = companyUserService.selectCompanyQwUserByDept(combinedList, loginUser.getUser().getUserType());
        if (userIds.isEmpty()){
            return AjaxResult.error();
        }

        fsCourseFinishTempParent.setUserIds(userIds);
        List<FsCourseFinishTempParent> list = fsCourseFinishTempParentService.selectFsCourseFinishTempParentList(fsCourseFinishTempParent);
        ExcelUtil<FsCourseFinishTempParent> util = new ExcelUtil<FsCourseFinishTempParent>(FsCourseFinishTempParent.class);
        return util.exportExcel(list, "完课模板数据");
    }


    /**
     * 获取完课模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:courseFinishTempParent:query') || @ss.hasPermi('course:courseFinishTempParent:myQuery') || @ss.hasPermi('course:courseFinishTempParent:deptQuery')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(fsCourseFinishTempParentService.selectFsCourseFinishTempParentById(id));
    }

    /**
     * 新增完课模板
     */
    @PreAuthorize("@ss.hasPermi('course:courseFinishTempParent:add') || @ss.hasPermi('course:courseFinishTempParent:myAdd') || @ss.hasPermi('course:courseFinishTempParent:deptAdd')")
    @Log(title = "完课模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsCourseFinishTempParent fsCourseFinishTempParent){

        LoginUser loginUser = SecurityUtils.getLoginUser();
        fsCourseFinishTempParent.setCompanyId(loginUser.getCompany().getCompanyId());
        fsCourseFinishTempParent.setCreateTime(new Date());
        fsCourseFinishTempParent.setCreateBy(String.valueOf(loginUser.getUser().getUserId()));
        return toAjax(fsCourseFinishTempParentService.insertFsCourseFinishTempParent(fsCourseFinishTempParent));
    }

    /**
     * 修改完课模板
     */
    @PreAuthorize("@ss.hasPermi('course:courseFinishTempParent:edit') || @ss.hasPermi('course:courseFinishTempParent:myEdit') || @ss.hasPermi('course:courseFinishTempParent:deptEdit')")
    @Log(title = "完课模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsCourseFinishTempParent fsCourseFinishTempParent)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fsCourseFinishTempParent.setCompanyId(loginUser.getCompany().getCompanyId());
        return toAjax(fsCourseFinishTempParentService.updateFsCourseFinishTempParent(fsCourseFinishTempParent));
    }

    /**
     * 删除完课模板
     */
    @PreAuthorize("@ss.hasPermi('course:courseFinishTempParent:remove') || @ss.hasPermi('course:courseFinishTempParent:myRemove') || @ss.hasPermi('course:courseFinishTempParent:deptRemove')")
    @Log(title = "完课模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fsCourseFinishTempParentService.deleteFsCourseFinishTempParentByIds(ids));
    }
}
