package com.bonus.system.att.controller;

import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.log.annotation.Log;
import com.bonus.common.log.enums.BusinessType;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.system.att.entity.AttGroupBean;
import com.bonus.system.att.service.AttGroupService;
import com.bonus.system.basic.domain.SysTree;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 考勤组
 *
 * @author zys
 */
@RestController
@RequestMapping("/attGroup")
@Slf4j
public class AttGroupController extends BaseController {

    @Resource(name = "attGroupService")
    private AttGroupService attGroupService;

    /**
     * 获取考勤组列表
     */
    @RequiresPermissions("att:group:list")
    @GetMapping("/list")
    @Log(title = "考勤设置->考勤规则设置->列表查询", businessType = BusinessType.QUERY)
    public TableDataInfo list(AttGroupBean bean) {
        try{
            startPage();
            return getDataTable(attGroupService.selectAttGroupList(bean));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return getDataTableError(new ArrayList<>());
    }

    /**
     * 新增考勤组
     */
    @RequiresPermissions("att:group:add")
    @PostMapping
    @Log(title = "考勤设置->考勤规则设置->考勤组新增", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody AttGroupBean bean) {
        try{
            int i = attGroupService.insertAttGroup(bean);
            if(i==-1){
                return error("考勤组没有人员或选择的组织下没有人员");
            }
            return toAjax(i);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    /**
     * 根据考勤组编号获取详细信息
     */
    @RequiresPermissions("att:group:query")
    @GetMapping(value = "/{groupId}")
    public AjaxResult getInfo(@PathVariable Long groupId) {
        try{
            return success(attGroupService.selectAttGroupById(groupId));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    /**
     * 修改考勤组
     */
    @RequiresPermissions("att:group:edit")
    @PutMapping
    @Log(title = "考勤设置->考勤规则设置->考勤组修改", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@RequestBody AttGroupBean bean) {
        try{
            int i = attGroupService.updateAttGroup(bean);
            if(i==-1){
                return error("考勤组没有人员或选择的组织下没有人员");
            }
            return toAjax(i);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    /**
     * 删除考勤组
     */
    @RequiresPermissions("att:group:remove")
    @DeleteMapping("/{groupId}")
    @Log(title = "考勤设置->考勤规则设置->考勤组删除", businessType = BusinessType.DELETE)
    public AjaxResult remove(@PathVariable("groupId") Long groupId) {
        try{
            return toAjax(attGroupService.deleteAttGroup(groupId));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    /**
     * 查询组织选择列表
     */
    @PostMapping("/selectOrgList")
    @Log(title = "考勤设置->查询组织选择列表", businessType = BusinessType.QUERY)
    public AjaxResult selectOrgList() {
        try{
            List<SysTree> sysTrees = attGroupService.selectOrgList();
            return success(sysTrees);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

}
