package com.zehong.web.controller.baseinfo;

import com.zehong.common.annotation.Log;
import com.zehong.common.constant.UserConstants;
import com.zehong.common.core.controller.BaseController;
import com.zehong.common.core.domain.AjaxResult;
import com.zehong.common.core.domain.entity.SysUser;
import com.zehong.common.core.page.TableDataInfo;
import com.zehong.common.enums.BusinessType;
import com.zehong.common.utils.SecurityUtils;
import com.zehong.common.utils.StringUtils;
import com.zehong.common.utils.poi.ExcelUtil;
import com.zehong.system.domain.baseinfo.TStaff;
import com.zehong.system.domain.form.TStaffForm;
import com.zehong.system.domain.vo.TStaffVo;
import com.zehong.system.domain.vo.UserCourseVo;
import com.zehong.system.service.system.ISysPostService;
import com.zehong.system.service.system.ISysRoleService;
import com.zehong.system.service.system.ISysUserService;
import com.zehong.system.service.baseinfo.ITStaffService;
import com.zehong.system.service.train.ITTrainCourseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工信息管理Controller
 *
 * @author zehong
 * @date 2022-06-17
 */
@RestController
@RequestMapping("/baseInfo/staff")
public class TStaffController extends BaseController
{
    @Autowired
    private ITStaffService tStaffService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysPostService postService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ITTrainCourseService tTrainCourseService;

    /**
     * 查询员工信息管理列表
     */
    //@PreAuthorize("@ss.hasPermi('safetyManagement:staff:list')")
    @GetMapping("/list")
    public TableDataInfo list(TStaffForm tStaff)
    {
        startPage();
        List<TStaffVo> list = tStaffService.selectTStaffList(tStaff);
        return getDataTable(list);
    }

    @GetMapping("/allList")
    public TableDataInfo allList(TStaffForm tStaff)
    {
        startPage();
        List<TStaffVo> list = tStaffService.allList(tStaff);
        return getDataTable(list);
    }
    /**
     * 无分页查询
     * @param tStaff
     * @return
     */
    @GetMapping("/TStaffList")
    public TableDataInfo TStaffList(TStaffForm tStaff)
    {
        List<TStaffVo> list = tStaffService.selectTStaffList(tStaff);
        return getDataTable(list);
    }

    /**
     * 导出员工信息管理列表
     */
    @Log(title = "员工信息管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TStaffForm tStaff)
    {
        List<TStaffVo> list = tStaffService.selectTStaffList(tStaff);
        ExcelUtil<TStaffVo> util = new ExcelUtil<TStaffVo>(TStaffVo.class);
        return util.exportExcel(list, "员工信息管理数据");
    }

    /**
     * 获取员工信息管理详细信息
     */
    @GetMapping(value = {"/", "/{staffId}"})
    public AjaxResult getInfo(@PathVariable(value = "staffId", required = false) Long staffId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("roles", roleService.selectRoleAll());
        ajax.put("posts", postService.selectPostAll());
        ajax.put("data", tStaffService.selectTStaffById(staffId));
        return ajax;
    }

    /**
     * 新增员工信息管理
     */
    @Log(title = "员工信息管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TStaff tStaff)
    {
        SysUser user=new SysUser();
        user.setPhonenumber(tStaff.getPhonenumber());
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(tStaff.getUserName()))) {
            return AjaxResult.error("新增用户'" + tStaff.getUserName() + "'失败，登录账号已存在");
        } else if (StringUtils.isNotEmpty(tStaff.getPhonenumber())
                && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
            return AjaxResult.error("新增用户'" + tStaff.getUserName() + "'失败，手机号码已存在");
        }
        //密码加密
        tStaff.setPassword(SecurityUtils.encryptPassword(tStaff.getPassword()));
        return toAjax(tStaffService.insertTStaff(tStaff));
    }

    /**
     * 修改员工信息管理
     */
    @Log(title = "员工信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TStaff tStaff)
    {
        return toAjax(tStaffService.updateTStaff(tStaff));
    }

    /**
     * 删除员工信息管理
     */
    @Log(title = "员工信息管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{staffIds}")
    public AjaxResult remove(@PathVariable Long[] staffIds)
    {
        return toAjax(tStaffService.deleteTStaffByIds(staffIds));
    }

    @ApiOperation("用户课程列表")
    @GetMapping("/userCourseList")
    public TableDataInfo userCourseList(TStaffForm tStaff){
        startPage();
        List<UserCourseVo> list = tTrainCourseService.userCourseLists(tStaff);
        return getDataTable(list);
    }

}
