package com.zhongzheng.controller.user;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.zhongzheng.common.core.domain.entity.SysUser;
import com.zhongzheng.modules.alisms.service.IAliSmsService;
import com.zhongzheng.modules.grade.vo.ClassPeriodStudentExportVo;
import com.zhongzheng.modules.user.bo.*;
import com.zhongzheng.modules.user.service.IUserService;
import com.zhongzheng.modules.user.vo.UserStudyRecordVo;
import com.zhongzheng.modules.user.vo.UserVo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhongzheng.common.annotation.Log;
import com.zhongzheng.common.core.controller.BaseController;
import com.zhongzheng.common.core.domain.AjaxResult;
import com.zhongzheng.common.enums.BusinessType;
import com.zhongzheng.common.utils.poi.ExcelUtil;
import com.zhongzheng.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 客户端用户Controller
 *
 * @author ruoyi
 * @date 2021-06-08
 */
@Api(value = "客户端用户控制器", tags = {"客户端用户管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/app/user")
public class UserController extends BaseController {

    private final IUserService iUserService;

    private final IAliSmsService iSmsService;

    /**
     * 查询客户端用户列表
     */
    @ApiOperation("查询客户端用户列表")
    @PreAuthorize("@ss.hasPermi('app:user:list')")
    @GetMapping("/list")
    public TableDataInfo<UserVo> list(UserQueryBo bo) {
        startPage();
        List<UserVo> list = iUserService.selectList(bo);
        return getDataTable(list);
    }

    /**
     * 导出客户端用户列表
     */
    @ApiOperation("导出客户端用户列表")
    @PreAuthorize("@ss.hasPermi('app:user:export')")
    @Log(title = "客户端用户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<UserVo> export(UserQueryBo bo) {
        List<UserVo> list = iUserService.queryList(bo);
        ExcelUtil<UserVo> util = new ExcelUtil<UserVo>(UserVo.class);
        ExportParams deptExportParams = new ExportParams();
        // 设置sheet得名称
        deptExportParams.setSheetName("表1");
        Map<String, Object> deptExportMap = new HashMap<>();
        deptExportMap.put("title", deptExportParams);
        deptExportMap.put("entity", UserVo.class);
        // sheet中要填充得数据
        deptExportMap.put("data", list);
        List<Map<String, Object>> sheetsList = new ArrayList<>();
        sheetsList.add(deptExportMap);
        return util.exportEasyExcel(sheetsList, "客户端用户");
    }

    /**
     * 获取客户端用户详细信息
     */
    @ApiOperation("获取客户端用户详细信息")
    @PreAuthorize("@ss.hasPermi('app:user:query')")
    @GetMapping("/{userId}")
    public AjaxResult<UserVo> getInfo(@PathVariable("userId" ) Long userId) {
        return AjaxResult.success(iUserService.queryById(userId));
    }

    /**
     * 新增客户端用户
     */
    @ApiOperation("新增客户端用户")
    @PreAuthorize("@ss.hasPermi('app:user:add')")
    @Log(title = "客户端用户", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Long> add(@RequestBody UserSystemRegisterBo bo) {
        return AjaxResult.success(iUserService.systemRegisterUser(bo));
    }

    /**
     * 修改客户端用户
     */
    @ApiOperation("修改客户端用户")
    @PreAuthorize("@ss.hasPermi('app:user:edit')")
    @Log(title = "客户端用户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult<Void> edit(@RequestBody UserEditBo bo) throws IllegalAccessException {
        return toAjax(iUserService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除客户端用户
     */
    @ApiOperation("删除客户端用户")
    @PreAuthorize("@ss.hasPermi('app:user:remove')")
    @Log(title = "客户端用户" , businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    public AjaxResult<Void> remove(@PathVariable Long[] userIds) {
        return toAjax(iUserService.deleteWithValidByIds(Arrays.asList(userIds), true) ? 1 : 0);
    }

    /**
     * 查询客户端用户学习记录列表
     */
    @ApiOperation("客户端用户学习记录列表")
    @PreAuthorize("@ss.hasPermi('app:user:studyRecordList')")
    @GetMapping("/studyRecordList")
    public TableDataInfo<UserStudyRecordVo> studyRecordList(UserStudyRecordQueryBo bo) {
        startPage();
        List<UserStudyRecordVo> list = iUserService.selectStudyRecordList(bo);
        return getDataTable(list);
    }

    @ApiOperation("测试")
    @PreAuthorize("@ss.hasPermi('app:user:studyRecordList')")
    @GetMapping("/test")
    public AjaxResult<Void> test(UserStudyRecordQueryBo bo) {
        iSmsService.sendPwdSms("15999946079","559195");
        return AjaxResult.success();
    }

}
