package com.ruoyi.web.controller.user;

import java.util.Date;
import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.enums.FilePathSplicingType;
import com.ruoyi.common.filepathsplicing.FilePathSplicing;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.enums.SequencePrefixEnum;
import com.ruoyi.system.service.ISysSequenceService;
import com.ruoyi.user.domain.User;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.user.domain.vo.UserVo;
import com.ruoyi.user.domain.bo.UserBo;
import com.ruoyi.user.service.IUserService;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;

/**
 * 小程序用户管理Controller
 *
 * @author wuchao
 * @date 2022-03-03
 */
@Validated
@Api(value = "小程序用户管理控制器", tags = {"小程序用户管理管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/miniapp/user")
public class UserController extends BaseController {

    private final IUserService iUserService;
    private final ISysSequenceService sysSequenceService;

    /**
     * 查询小程序用户管理列表
     */
    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
    @ApiOperation("查询小程序用户管理列表")
    @SaCheckPermission("system:miniapp:user:page")
    @GetMapping("/page")
    public TableDataInfo<UserVo> page(@Validated(QueryGroup.class) UserBo bo, PageQuery pageQuery) {
        return iUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询小程序用户管理列表
     */
    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
    @ApiOperation("查询小程序用户管理列表")
    @SaCheckPermission("system:miniapp:user:list")
    @GetMapping("/list")
    public R<List<UserVo>> list(@Validated(QueryGroup.class) UserBo bo) {
        List<UserVo> result = iUserService.queryListForSelect(bo);
        return R.ok(result);
    }

    /**
     * 获取小程序用户管理详细信息
     */
    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
    @ApiOperation("获取小程序用户管理详细信息")
    @SaCheckPermission("system:miniapp:user:query")
    @GetMapping("/{id}")
    public R<UserVo> getInfo(@ApiParam("主键") @NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return R.ok(iUserService.queryById(id));
    }

    /**
     * 新增小程序用户管理
     */
    @ApiOperation("新增小程序用户管理")
    @SaCheckPermission("system:miniapp:user:add")
    @Log(title = "小程序用户管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UserBo bo) {
        bo.setBusinessId(sysSequenceService.generateSequence(SequencePrefixEnum.PRE_USER, DateUtil.format(new Date(), "yyMMdd"), 6));
        return toAjax(iUserService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改小程序用户管理
     */
    @ApiOperation("修改小程序用户管理")
    @SaCheckPermission("system:miniapp:user:edit")
    @Log(title = "小程序用户管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserBo bo) {
        return toAjax(iUserService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除小程序用户管理
     */
    @ApiOperation("删除小程序用户管理")
    @SaCheckPermission("system:miniapp:user:remove")
    @Log(title = "小程序用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串") @NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iUserService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 导出小程序用户管理列表
     */
    @ApiOperation("导出小程序用户管理列表")
    @SaCheckPermission("system:miniapp:user:export")
    @Log(title = "小程序用户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated UserBo bo, HttpServletResponse response) {
        List<UserVo> list = iUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "小程序用户管理", UserVo.class, response);
    }

    /**
     * 更新业务id
     */
    @RepeatSubmit()
    @PostMapping("/updateBusinessId")
    public R<Void> updateBusinessId() {
        List<UserVo> userVos = iUserService.queryList(new UserBo());
        List<User> users = userVos.stream().filter(userVo -> StringUtils.isEmpty(userVo.getBusinessId())).map(userVo -> {
            User user = new User();
            user.setId(userVo.getId());
            user.setBusinessId(sysSequenceService.generateSequence(SequencePrefixEnum.PRE_USER, DateUtil.format(new Date(), "yyMMdd"), 6));
            return user;
        }).collect(Collectors.toList());
        iUserService.updateBusinessId(users);
        return R.ok();
    }


}
