package com.bonus.system.att.controller;

import com.bonus.common.core.utils.StringUtils;
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.common.security.utils.SecurityUtils;
import com.bonus.system.att.entity.OrgChangeBean;
import com.bonus.system.att.service.OrgChangeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * 组织架构变更
 *
 * @author fly
 */
@RestController
@RequestMapping("/orgChange")
@Slf4j
public class OrgChangeController extends BaseController {

    @Resource(name = "OrgChangeService")
    private OrgChangeService orgChangeService;

    /**
     * 获取组织架构列表
     */
//    @RequiresPermissions("att:org:change:list")
    @GetMapping("/list")
    @Log(title = "考勤设置->组织架构变更->列表查询", businessType = BusinessType.QUERY)
    public TableDataInfo list(OrgChangeBean bean) {
        try{
            startPage();
            //只查自己的
            Long userId = SecurityUtils.getLoginUser().getSysUser().getUserId();
            bean.setUserId(userId);
            return getDataTable(orgChangeService.selectOrgChangeList(bean));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return getDataTableError(new ArrayList<>());
    }


    /**
     * 获取组织架构变更列表 - 人资
     */
//    @RequiresPermissions("att:org:change:list")
    @GetMapping("/getAllList")
    @Log(title = "考勤设置->组织架构变更->列表查询人资", businessType = BusinessType.QUERY)
    public TableDataInfo getAllList(OrgChangeBean bean) {
        try{
            startPage();
            if(StringUtils.isEmpty(bean.getIsCheck())){
                //不查撤回的
                bean.setIsCheck("-1");
            }
            return getDataTable(orgChangeService.selectOrgChangeList(bean));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return getDataTableError(new ArrayList<>());
    }

    /**
     * 新增组织架构变更单
     */
//    @RequiresPermissions("att:org:change:add")
    @PostMapping
    @Log(title = "考勤设置->组织架构变更->组织架构新增", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody OrgChangeBean bean) {
        try{
            return toAjax(orgChangeService.insertOrgChange(bean));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    /**
     * 根据组织架构编号获取详细信息
     */
//    @RequiresPermissions("att:org:change:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        try{
            return success(orgChangeService.selectOrgChangeById(id));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    /**
     * 修改组织架构
     */
//    @RequiresPermissions("att:org:change:edit")
    @PutMapping
    @Log(title = "考勤设置->组织架构变更->组织架构修改", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@RequestBody OrgChangeBean bean) {
        try{
            return toAjax(orgChangeService.updateOrgChange(bean));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    /**
     * 删除组织架构
     */
//    @RequiresPermissions("att:org:change:remove")
    @DeleteMapping("/{id}")
    @Log(title = "考勤设置->组织架构变更->组织架构删除", businessType = BusinessType.DELETE)
    public AjaxResult remove(@PathVariable("id") Long id) {
        try{
            return toAjax(orgChangeService.deleteAttGroup(id));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    /**
     * 提交/撤回 审核
     */
//    @RequiresPermissions("att:org:change:edit")
    @PutMapping("orgChangeCheck")
    @Log(title = "考勤设置->组织架构变更->组织架构审核", businessType = BusinessType.UPDATE)
    public AjaxResult orgChangeCheck(@Validated @RequestBody OrgChangeBean o) {
        try{
            return toAjax(orgChangeService.orgChangeCheck(o));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    @PostMapping("orgBatchChangeCheck")
    @Log(title = "流程相关->组织架构变更审核->一键审核", businessType = BusinessType.UPDATE)
    public AjaxResult orgBatchChangeCheck(@Validated @RequestBody OrgChangeBean o) {
        try{
            if(CollectionUtils.isEmpty(o.getIds())){
                return AjaxResult.error("请选择要审批的数据");
            }
            for (Long id : o.getIds()) {
                o.setId(id);
                orgChangeService.orgChangeCheck(o);
            }
            return success();
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    /**
     * 获取考勤组
     */
//    @RequiresPermissions("att:org:change:edit")
    @GetMapping("getAttGroupList")
    @Log(title = "考勤设置->组织架构变更->组织架构审核", businessType = BusinessType.UPDATE)
    public AjaxResult getAttGroupList() {
        try{
            return success(orgChangeService.getAttGroupList());
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

}
