package com.sckw.system.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.core.annotation.RepeatSubmit;
import com.sckw.core.common.enums.enums.DictEnum;
import com.sckw.core.exception.SystemException;
import com.sckw.core.model.page.PageHelperUtil;
import com.sckw.core.model.page.PageResult;
import com.sckw.core.utils.StringUtils;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.response.HttpResult;
import com.sckw.redis.constant.RedisConstant;
import com.sckw.redis.utils.RedissonUtils;
import com.sckw.system.api.model.dto.req.RegisterReqDto;
import com.sckw.system.api.model.dto.req.UpdatePasswordReqDto;
import com.sckw.system.model.KwsUser;
import com.sckw.system.model.vo.req.ForgetPasswordReqVo;
import com.sckw.system.model.vo.req.ResetPasswordReqVo;
import com.sckw.system.model.vo.req.UserAddReqVo;
import com.sckw.system.model.vo.res.KwsUserResVo;
import com.sckw.system.service.KwsUserService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 用户
 *
 * @author zk
 * @date 2023-05-30
 */
@RestController
@RequestMapping("/kwsUser")
public class KwsUserController {

    @Autowired
    private KwsUserService kwsUserService;

    /**
     * @param id 主键ID
     * @description 根据主键查询
     * @author zk
     * @date 2023/5/30
     **/
    @GetMapping("/detail")
    public HttpResult selectByKey(Long id) {
        return HttpResult.ok(kwsUserService.selectByKey(id));
    }

    /**
     * @param params {page：页数、pageSize：每页条数、。。。}
     * @description 分页查询
     * @author zk
     * @date 2023/5/30
     **/
    @PostMapping("/select")
    public HttpResult findPage(@RequestBody HashMap params) {
        // 设置分页参数
        PageHelper.startPage(PageResult.getPage(params), PageResult.getPageSize(params));
        List<KwsUserResVo> list = kwsUserService.findPage(params);
        PageResult pageResult = PageHelperUtil.getPageResult(new PageInfo<>(list));
        return HttpResult.ok(pageResult);
    }

    /**
     * @param params {page：页数、pageSize：每页条数、。。。}
     * @description 分页查询
     * @author zk
     * @date 2023/5/30
     **/
    @PostMapping("/findList")
    public HttpResult findList(@RequestBody Map<String, Object> params) {
        return HttpResult.ok(kwsUserService.findList(params));
    }

    /**
     * @param reqVo 新增用户入参
     * @return HttpResult
     * @desc: 新增用户
     * @author: czh
     * @date: 2023/6/14
     */
    @PostMapping("/add")
    @RepeatSubmit(interval = 3000,message ="两次请求间隔未超过3秒")
    public HttpResult add(@Valid @RequestBody UserAddReqVo reqVo,
                          @RequestHeader(name = "System-Type", required = true) int systemType) {
        reqVo.setSystemType(systemType);
        kwsUserService.add(reqVo);
        return HttpResult.ok(HttpStatus.MSG_003);
    }

    /**
     * @param params {id：主键ID、systemType:系统类型(1运营端、2企业开户)、account：账号、password：密码、name：姓名、phone：联系电话
     *               *               ，photo：头像，email：邮箱}
     * @description 更新
     * @author zk
     * @date 2023/5/30
     **/
    @PostMapping("/update")
    public HttpResult update(@Valid @RequestBody UserAddReqVo params) {
        kwsUserService.update(params);
        return HttpResult.ok(HttpStatus.MSG_005);
    }

    /**
     * @param id
     * @description 注销账户
     * @author zk
     * @date 2023/06/02
     **/
    @GetMapping(value = "/closeAccount")
    public HttpResult closeAccount(@RequestParam Long id) {
        kwsUserService.closeAccount(id);
        return HttpResult.ok(HttpStatus.MSG_008);
    }

    /**
     * @param ids {ids：主键ID（多个以逗号隔开）}
     * @description 更新
     * @author zk
     * @date 2023/5/30
     **/
    @PostMapping("/dels")
    public HttpResult del(@RequestParam String ids) {
        kwsUserService.deleteByKey(ids);
        return HttpResult.ok(HttpStatus.MSG_008);
    }


    /**
     * @param reqVo 忘记密码入参
     * @return HttpResult
     * @desc: 忘记密码
     * @author: czh
     * @date: 2023/6/19
     */
    @PostMapping("/forgetPassword")
    public HttpResult forgetPassword(@Valid @RequestBody ForgetPasswordReqVo reqVo,
                                     @RequestHeader(name = "System-Type", required = true) int systemType) {
        reqVo.setSystemType(systemType);
        kwsUserService.forgetPassword(reqVo);
        return HttpResult.ok(HttpStatus.MSG_002);
    }

    /**
     * @param reqVo 重置入参
     * @return HttpResult
     * @desc: 重置密码
     * @author: czh
     * @date: 2023/7/14
     */
    @PostMapping("resetPassword")
    public HttpResult resetPassword(@RequestBody ResetPasswordReqVo reqVo) {
        kwsUserService.resetPasswordByMain(reqVo);
        return HttpResult.ok(HttpStatus.MSG_005);
    }

    /**
     * @desc: 修改密码
     * @author: czh
     * @date: 2023/10/11
     */
    @PostMapping("/updatePassword")
    public HttpResult updatePassword(@RequestBody UpdatePasswordReqDto reqVo) {
        kwsUserService.updatePassword(reqVo);
        return HttpResult.ok(HttpStatus.MSG_002);
    }

    /**
     * @param reqVo 注册
     * @return HttpResult
     * @desc: 用户注册
     * @author: czh
     * @date: 2023/6/16
     */
    @PostMapping("/register")
    public HttpResult register(@Valid @RequestBody RegisterReqDto reqVo,
                               @RequestHeader(name = "System-Type", required = true) int systemType) {
        reqVo.setSystemType(systemType);

        String key = StringUtils.format(RedisConstant.MESSAGE_SMS_VERIFY_CODE_VALUE_KEY, DictEnum.SMS_REGISTER.getValue(), reqVo.getPhone());
        String sms = RedissonUtils.getString(key);
        if (!reqVo.getCaptcha().equals(sms)) {
            throw new SystemException(HttpStatus.CODE_10301, HttpStatus.CAPTCHA_ERROR);
        }
        kwsUserService.register(reqVo);
        return HttpResult.ok(HttpStatus.MSG_007);
    }

    /**
     * @param ids 主键
     * @return HttpResult
     * @desc: 冻结
     * @author: czh
     * @date: 2023/7/12
     */
    @GetMapping("freeze")
    public HttpResult freeze(@RequestParam String ids) {
        kwsUserService.freeze(ids);
        return HttpResult.ok(HttpStatus.MSG_005);
    }

    /**
     * @param ids 主键
     * @return HttpResult
     * @desc: 解冻
     * @author: czh
     * @date: 2023/7/12
     */
    @GetMapping("unFreeze")
    public HttpResult unFreeze(@RequestParam String ids) {
        kwsUserService.unFreeze(ids);
        return HttpResult.ok(HttpStatus.MSG_005);
    }


    /**
     * @desc: 导出
     * @author: czh
     * @date: 2023/7/11
     */
    @PostMapping(value = "export", produces = MediaType.APPLICATION_JSON_VALUE)
    public void export(@RequestBody HashMap params, HttpServletResponse response) {
        kwsUserService.export(params, response);
    }



    @PostMapping("testEdit")
    public HttpResult testEdit(@RequestParam String type) {
        return HttpResult.ok(kwsUserService.test(Arrays.stream(type.split(",")).map(Integer::parseInt).toList()));
    }

    @RepeatSubmit
    @PostMapping("testApprove")
    public HttpResult testApprove(@RequestBody ForgetPasswordReqVo reqVo) {
        kwsUserService.test(Collections.singletonList(1));
        return HttpResult.ok(HttpStatus.MSG_004);
    }
}
