package com.bonus.boot.manager.basic.controller;

import com.bonus.boot.manager.basic.dao.PersonnelLibraryDao;
import com.bonus.boot.manager.basic.entity.PersonnelLibraryBean;
import com.bonus.boot.manager.basic.entity.ZNode;
import com.bonus.boot.manager.basic.service.PersonnelLibraryService;
import com.bonus.boot.manager.manager.annotation.LogAnnotation;
import com.bonus.boot.manager.manager.entity.R;
import com.bonus.boot.manager.manager.utils.AjaxRes;
import com.bonus.boot.manager.manager.utils.GlobalConst;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 包名称：com.bonus.boot.manager.basic.controller
 * 类名称：PersonnelLibraryController
 * 类描述：体检人员库
 * 创建人：@author tqzhang
 * 创建时间：2023年05月31日 17:10
 */


@RestController
@RequestMapping("personnelLibrary")
public class PersonnelLibraryController {
    @Resource
    private PersonnelLibraryService service;

    @Resource
    private PersonnelLibraryDao daoDao;

    @LogAnnotation
    @PostMapping("getMsgContent")
    @ApiOperation(value = "体检人员库-列表")
    public R getList(PersonnelLibraryBean o) {
        List<PersonnelLibraryBean> list = service.getList(o);
        return list.size() > 0 ? R.okTable(list, list.size()) : R.failTable("暂无数据");
    }

    @RequestMapping(value = "addInfo", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "体检人员库-新增")
    public AjaxRes addInfo(@RequestBody PersonnelLibraryBean bean) {
        AjaxRes ar = new AjaxRes();
        int cb = service.addInfo(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if (cb > 0) {
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
            if (cb == 3) {
                ar.setFailMsg("人员已存在");
            }
            if (cb == 4) {
                ar.setFailMsg("人员年龄不符合标准");
            }
            if (cb == 5) {
                ar.setFailMsg("手机号已存在");
            }
            if (cb == 6) {
                ar.setFailMsg("姓名已存在");
            }
        }
        return ar;
    }

    @RequestMapping(value = "updateInfo", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "体检人员库-修改")
    public AjaxRes updateInfo(@RequestBody PersonnelLibraryBean bean) {
        AjaxRes ar = new AjaxRes();
        int cb = service.updateInfo(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if (cb > 0) {
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
            if (cb == 3) {
                ar.setFailMsg("人员已存在");
            }
            if (cb == 5) {
                ar.setFailMsg("手机号已存在");
            }

        }
        return ar;
    }

    @RequestMapping(value = "getListById", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "体检人员库-查询人员信息")
    public AjaxRes getListById(String id) {
        AjaxRes ar = new AjaxRes();
        PersonnelLibraryBean PersonnelLibraryBean = service.getListById(id);
        Map<String, Object> map = new HashMap<>();
        if (PersonnelLibraryBean != null) {
            map.put("PersonnelLibraryBean", PersonnelLibraryBean);
        }
        ar.setSucceed(map, GlobalConst.DATA_SUCCEED);
        return ar;
    }


    @LogAnnotation
    @PostMapping("delById")
    @ApiOperation(value = "体检人员库-删除")
    public AjaxRes delById(PersonnelLibraryBean bean) {
        AjaxRes ar = new AjaxRes();
        int result = service.delById(bean);
        if (result == 1) {
            ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
        } else {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }

    @LogAnnotation
    @PostMapping("resetPassword")
    @ApiOperation(value = "体检人员库-删除")
    public AjaxRes resetPassword(PersonnelLibraryBean bean) {
        AjaxRes ar = new AjaxRes();
        int result = service.resetPassword(bean);
        if (result == 1) {
            ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
        } else {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }

    @LogAnnotation
    @PostMapping("getDepartmentTree")
    @ApiOperation(value = "体检人员库-获取部门树")
    public AjaxRes getDepartmentTree() {
        AjaxRes ar = new AjaxRes();
        try {
            List<ZNode> list = service.getDepartmentTree();
            ar.setSucceed(list);
        } catch (Exception e) {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }

}
