package com.dcsoft.system.kq.controller;


import com.dcsoft.common.core.utils.poi.ExcelUtil;
import com.dcsoft.common.core.web.controller.BaseController;
import com.dcsoft.common.core.web.domain.AjaxResult;
import com.dcsoft.common.core.web.page.TableDataInfo;
import com.dcsoft.common.log.annotation.Log;
import com.dcsoft.common.log.enums.BusinessType;
import com.dcsoft.common.security.annotation.RequiresPermissions;
import com.dcsoft.system.kq.domain.KqClasses;
import com.dcsoft.system.kq.domain.KqClassesItem;
import com.dcsoft.system.kq.service.IKqClassesItemService;
import com.dcsoft.system.kq.service.IKqClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 考勤班次Controller
 *
 * @author dcsoft
 * @date 2023-01-06
 */
@RestController
@RequestMapping("/kqClasses")
public class KqClassesController extends BaseController
{
    @Autowired
    private IKqClassesService kqClassesService;

    @Autowired
    private IKqClassesItemService kqClassesItemService;

    /**
     * 查询考勤班次列表
     */
    @RequiresPermissions("kq:classes:list")
    @GetMapping("/list")
    public TableDataInfo list(KqClasses kqClasses)
    {
        startPage();
        List<KqClasses> list = kqClassesService.selectKqClassesList(kqClasses);
        return getDataTable(list);
    }

    /**
     * 导出考勤班次列表
     */
    @RequiresPermissions("kq:classes:export")
    @Log(title = "考勤班次", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KqClasses kqClasses)
    {
        List<KqClasses> list = kqClassesService.selectKqClassesList(kqClasses);
        ExcelUtil<KqClasses> util = new ExcelUtil<KqClasses>(KqClasses.class);
        util.exportExcel(response, list, "考勤班次数据");
    }

    /**
     * 获取考勤班次详细信息
     */
    @RequiresPermissions("kq:classes:query")
    @GetMapping(value = "/{classesId}")
    public AjaxResult getInfo(@PathVariable("classesId") Long classesId)
    {
        KqClasses kqClasses=kqClassesService.selectKqClassesByClassesId(classesId);
        KqClassesItem kqClassesItem=new KqClassesItem();
        kqClassesItem.setClassesId(classesId);
        List<KqClassesItem> item=kqClassesItemService.selectKqClassesItemList(kqClassesItem);
        kqClasses.setItem(item);
        return success(kqClasses);
    }

    /**
     * 新增考勤班次
     */
    @RequiresPermissions("kq:classes:add")
    @Log(title = "考勤班次", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KqClasses kqClasses)
    {
        int i=0;
        i=kqClassesService.insertKqClasses(kqClasses);
        List<KqClassesItem> item=kqClasses.getItem();
        for(KqClassesItem a:item){
            a.setClassesId(kqClasses.getClassesId());
            kqClassesItemService.insertKqClassesItem(a);
        }
        return toAjax(i);
    }

    /**
     * 修改考勤班次
     */
    @RequiresPermissions("kq:classes:edit")
    @Log(title = "考勤班次", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KqClasses kqClasses)
    {
        int i=0;
        i=kqClassesService.updateKqClasses(kqClasses);
        //删除子项
        kqClassesItemService.deleteKqClassesItemByClassesId(kqClasses.getClassesId());
        List<KqClassesItem> item=kqClasses.getItem();
        for(KqClassesItem a:item){
            a.setClassesId(kqClasses.getClassesId());
            kqClassesItemService.insertKqClassesItem(a);
        }
        return toAjax(i);
    }

    /**
     * 删除考勤班次
     */
    @RequiresPermissions("kq:classes:remove")
    @Log(title = "考勤班次", businessType = BusinessType.DELETE)
	@DeleteMapping("/{classesIds}")
    public AjaxResult remove(@PathVariable Long[] classesIds)
    {
        for(Long id:classesIds){
            kqClassesItemService.deleteKqClassesItemByClassesId(id);
        }
        return toAjax(kqClassesService.deleteKqClassesByClassesIds(classesIds));
    }
}
