package com.yonge.cooleshow.tenant.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.entity.TenantInfo;
import com.yonge.cooleshow.biz.dal.entity.TenantStaff;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.mapper.SysUserMapper;
import com.yonge.cooleshow.biz.dal.service.SmsCodeService;
import com.yonge.cooleshow.biz.dal.service.TenantInfoService;
import com.yonge.cooleshow.biz.dal.service.TenantStaffService;
import com.yonge.cooleshow.biz.dal.wrapper.TenantInfoWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.cooleshow.common.security.SecurityConstants;
import com.yonge.cooleshow.tenant.io.request.SysUserWrapper;
import com.yonge.cooleshow.tenant.io.request.TenantInfoVo;
import com.yonge.toolset.base.exception.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author：haonan
 * @Date：2023/7/24 9:57
 * @Filename：TenantInfoController
 */
@RestController
@RequestMapping("${app-config.url.tenant:}/tenantInfoUpdate")
@Api(value = "机构信息修改", tags = "机构信息修改")
public class TenantInfoController extends BaseController {

    @Autowired
    TenantInfoService tenantInfoService;

    @Autowired
    SysUserFeignService sysUserFeignService;

    @Autowired
    private SmsCodeService smsCodeService;

    @Autowired
    private TenantStaffService tenantStaffService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @ApiOperation(value = "发送登录短信验证码")
    @ApiImplicitParams({@ApiImplicitParam(name = "mobile", value = "手机号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "type", value = "类型（PASSWD:修改密码，LOGIN:登录或注册,BANK:绑定银行卡,PHONE:修改手机号）", required =
                    true, dataType = "String")})
    @PostMapping(value = "/sendSmsCode")
    public Object sendLoginVerifyCode(String mobile, String type,String code) throws Exception {
        smsCodeService.sendValidCode(mobile, type, ClientEnum.SYSTEM, code);
        return succeed();
    }

    @ApiOperation(value = "校验短信验证码")
    @ApiImplicitParams({@ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "code", value = "短信验证码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "type", value = "类型（PASSWD:修改密码，LOGIN:登录或注册,BANK:绑定银行卡,PHONE:修改手机号）", required =
                    true, dataType = "String")})
    @PostMapping(value = "/verifySmsCode")
    public Object verifySmsCode(String phone, String code, String type) {
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)) {
            return failed(SecurityConstants.PARAM_VERIFY_EXCEPTION);
        }
        if (smsCodeService.verifyValidCode(phone, code, type)) {
            return succeed();
        }
        return failed("验证码校验失败");
    }


    /**
     * 机构修改数据
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改机构信息", notes = "传入TenantInfo")
//    @PreAuthorize("@pcs.hasPermissions('tenantInfoUpdate/update')")
    public HttpResponseResult<Boolean> updateTenantInfo(@Valid @RequestBody TenantInfoVo.TenantInfo info) {

        TenantInfo tenantInfo = JSON.parseObject(info.jsonString(), TenantInfo.class);
        tenantInfoService.updateById(tenantInfo);

        return  HttpResponseResult.succeed(true);

    }




    /**
     * 当前机构查询详细
     */
    @PostMapping("/detail")
    @ApiOperation(value = "查询详细", notes = "查询详细")
//    @PreAuthorize("@pcs.hasPermissions('tenantInfoUpdate/detail')")
    public HttpResponseResult<TenantInfoWrapper.TenantInfo> detail() {
        TenantInfoWrapper.TenantInfo info = new TenantInfoWrapper.TenantInfo();
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        Long id = sysUser.getId();
        List<TenantStaff> list = tenantStaffService.lambdaQuery().eq(TenantStaff::getUserId, id).list();
        if (CollectionUtils.isNotEmpty(list)){
            TenantStaff staff = list.get(0);
            Long tenantId = staff.getTenantId();
            if (!(tenantId == null)){
                info = tenantInfoService.detailTenantInfo(tenantId);
            }
        }
        return succeed(info);
    }



    /**
     * 当前用户查询详细
     */
    @PostMapping("/sysUserDetail")
    @ApiOperation(value = "查询详细", notes = "查询详细")
//    @PreAuthorize("@pcs.hasPermissions('tenantInfoUpdate/sysUserDetail')")
    public HttpResponseResult<SysUser> sysUserDetail() {

        SysUser sysUser = sysUserFeignService.queryUserInfo();


        return HttpResponseResult.succeed(sysUser);
    }

    /**
     * 当前用户修改信息
     */
    @PostMapping("/sysUserUpdate")
    @ApiOperation(value = "用户修改", notes = "用户修改")
//    @PreAuthorize("@pcs.hasPermissions('tenantInfoUpdate/sysUserUpdate')")
    public HttpResponseResult<Boolean> sysUserUpdate(@RequestBody SysUserWrapper.SysUser sysUser) {

        // 当前用户请登录，提示请登录
        SysUser user = sysUserFeignService.queryUserInfo();
        if (Objects.isNull(user)) {
            throw new BizException("请登录");
        }

        TenantStaff staff = tenantStaffService.getByUserId(user.getId());
        if (staff == null) {
            throw new BizException("账号不存在");
        }
        // 设置用户机构ID
        user.setTenantId(staff.getTenantId());
        //如果传手机号
        if (StringUtils.isNotBlank(sysUser.getPhone())) {

            if (StringUtils.isBlank(sysUser.getCode())) {
                throw new BizException("验证码不能为空");
            }

            boolean exists = smsCodeService.verifyValidCode(sysUser.getPhone(), sysUser.getCode(), "PHONE");
            if (Boolean.FALSE.equals(exists)) {
                throw new BizException("验证码错误");
            }

            // 如果修改了手机号
            if (StringUtils.isNotEmpty(sysUser.getPhone()) && !user.getPhone().equals(sysUser.getPhone())) {
                // 查询输入的手机号  是否存在于sysUser表中
                SysUser sysUserInfo = sysUserMapper.findUserByPhone(sysUser.getPhone());
                if (Objects.nonNull(sysUserInfo)) {
                    throw new BizException("手机号已经注册平台账号");
                }
                // 清除登录token信息,有wxOpenId说明登陆过
                if (StringUtils.isNotEmpty(staff.getWxOpenid())) {
                    sysUserFeignService.exitByPhoneAndOpenId(ClientEnum.ORGANIZATION.getCode().toLowerCase(),
                            user.getPhone(), staff.getWxOpenid());
                    // 清除WXOpenId
                    tenantStaffService.update(null, Wrappers.<TenantStaff>lambdaUpdate()
                            .set(TenantStaff::getWxOpenid, null)
                            .eq(TenantStaff::getId, staff.getId()));
                }
            }
        }

        // 修改用户基础信息流程
        // 更新用户头象
        com.yonge.cooleshow.biz.dal.entity.SysUser updateSysUser = new com.yonge.cooleshow.biz.dal.entity.SysUser();
        updateSysUser.setId(user.getId());
        // 用户昵称
        if (StringUtils.isNotBlank(sysUser.getUsername())) {
            updateSysUser.setUsername(sysUser.getUsername());
        }
        //设置默认头像
        if (StringUtils.isNotBlank(sysUser.getAvatar())) {
            updateSysUser.setAvatar(sysUser.getAvatar());
        }
        if (StringUtils.isNotBlank(sysUser.getPhone())) {
            updateSysUser.setPhone(sysUser.getPhone());
        }
        updateSysUser.setUpdateTime(LocalDateTime.now());
        sysUserMapper.updateById(updateSysUser);


        // 更新机构员工头像和昵称，机构信息表中的用户昵称
        tenantStaffService.lambdaUpdate()
                .eq(TenantStaff::getId, staff.getId())
                .set(StringUtils.isNotEmpty(sysUser.getUsername()), TenantStaff::getNickname, sysUser.getUsername())
                .set(StringUtils.isNotEmpty(sysUser.getAvatar()), TenantStaff::getAvatar, sysUser.getAvatar())
                .set(TenantStaff::getUpdateTime, LocalDateTime.now())
                .update();

        // 更新机构信息表中的用户昵称
        tenantInfoService.lambdaUpdate()
                .eq(TenantInfo::getId, staff.getTenantId())
                .set(StringUtils.isNotEmpty(sysUser.getUsername()), TenantInfo::getUsername, sysUser.getUsername())
                .set(StringUtils.isNotEmpty(sysUser.getPhone()), TenantInfo::getPhone, sysUser.getPhone())
                .set(TenantInfo::getUpdateTime, new Date())
                .update();
        return HttpResponseResult.succeed(true);
    }
}
