package com.pj.project.app_user;

import java.util.Date;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.StaticLog;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.pj.api.consts.FeignFactory;
import com.pj.api.dto.*;

import com.pj.api.dto.BindShopDTO;
import com.pj.project.app_user.dto.PassDto;
import com.pj.utils.so.SoMap;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.pj.utils.sg.*;
import com.pj.project4sp.SP;

import cn.dev33.satoken.annotation.SaCheckPermission;


/**
 * Controller: app_user -- 移动端账号
 *
 * @author qzy
 */
@RestController
@RequestMapping("/AppUser/")
public class AppUserController {

    /**
     * 底层 Service 对象
     */
    @Autowired
    AppUserService appUserService;






    /**
     * 修改密码
     */
    @RequestMapping("pass")
    @SaCheckPermission(AppUser.PERMISSION_CODE_EDIT)
    public AjaxJson pass(@Validated PassDto dto) {
        StaticLog.info("dto:", JSONUtil.toJsonStr(dto.getPassword()));
        return AjaxJson.toAjax(appUserService.pass(dto));
    }
    /**
     * 修改密码
     */
    @RequestMapping("updateRole")
    @SaCheckPermission(AppUser.PERMISSION_CODE_EDIT)
    public AjaxJson updateRole(long id, String userType) {
        int line = SP.publicMapper.updateColumnById("app_user", "user_type", userType, id);
        return AjaxJson.getByLine(line);
    }

    /**
     * 查 - 根据id
     */
    @RequestMapping("getById")
    @SaCheckPermission(AppUser.PERMISSION_CODE)
    public AjaxJson getById(Long id) {
        AppUser a = appUserService.getById(id);
        return AjaxJson.getSuccessData(a);
    }

    /**
     * 查集合 - 根据条件（参数为空时代表忽略指定条件）
     */
    @RequestMapping("getList")
    @SaCheckPermission(AppUser.PERMISSION_CODE)
    public AjaxJson getList() {
        SoMap so = SoMap.getRequestSoMap();
        List<AppUser> list = appUserService.getList(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);
    }



    @RequestMapping("cleanFace")
    public AjaxJson cleanFace(@RequestParam("id") Long id) {
        AppUserDto appUserDto = FeignFactory.adminInterface.getByFkId(id);
        String faceId = appUserDto.getFaceId();
        if (StrUtil.isEmpty(faceId)) {
            return AjaxJson.getSuccess();
        }
        appUserService.cleanFace(faceId);
        return AjaxJson.getSuccess();
    }






    /*-----               正常业务接口👆👆    不越界     rpc远程调用 👇👇                     -----*/


    /**
     * 启/停边民的app账号登陆限制
     */
    @PostMapping("rpc/isLock")
    public int isLock(@RequestParam("id") String id,
                      @RequestParam("type") Integer type,
                      @RequestParam("status") Integer status) {
        int lock = appUserService.isLock(id, type, status);
        return lock;
    }

    /**
     * 查单个app有效用户 - 根据id
     */
    @RequestMapping("rpc/getById")
    public AppUserDto getAppUserById(@RequestParam("id") Long id) {
        AppUserDto appUser = appUserService.getUserById(id);
        return appUser;
    }

    /**
     * 商户认证
     */
    @PostMapping("rpc/audit")
    public boolean audit(@RequestBody EnterpriseAuditDto dto) {
        LambdaUpdateWrapper<AppUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(AppUser::getAuth, dto.getAuth()).set(AppUser::getAuthTime, new Date()).eq(AppUser::getFkId, dto.getFkId());
        boolean update = appUserService.update(updateWrapper);
        return update;
    }

    /**
     * 收购商认证
     */
    @PostMapping("rpc/saveAppUserInfo")
    public Boolean saveAppUserInfo(@RequestBody AppUserDto appUser) {
        boolean info = appUserService.saveAppUserInfo(appUser);
        return info;
    }


    /**
     * 用户数量统计
     *
     * @return
     */
    @RequestMapping("selectUsers")
    public AjaxJson selectUsers() {
        return AjaxJson.getSuccessData(appUserService.selectUsers());
    }


    /**
     * 创建账号
     *
     * @param createDTO
     */
    @PostMapping("rpc/createAppAccount")
    public boolean createAccount(@RequestBody AppUserCreateDTO createDTO) {
        return appUserService.createUser(createDTO);
    }

    @PostMapping("rpc/changeBindShopStatus")
    public void changeBindShopStatus(@RequestBody BindShopDTO bindShopDTO) {
        appUserService.changeBindShopStatus(bindShopDTO);
    }

    /**
     * 根据fkId查app用户
     */
    @PostMapping("rpc/getByFkId")
    public AppUserDto getAppUserByFkId(@RequestParam("fkId") Long fkId) {
        return appUserService.getAppUserByFkId(fkId);
    }

    @RequestMapping("rpc/findUserByPhone")
    public AppUserDto findUserByPhone(@RequestParam("phone") String phone) {
        return appUserService.findUserByPhone(phone);
    }

    @RequestMapping("rpc/bindFkId")
    public boolean bindFkId(@RequestParam("appUserId") Long appUserId, @RequestParam("fkId") Long fkId) {
        return appUserService.bindFkId(appUserId, fkId);
    }

    @PostMapping("rpc/updateAccount")
    public int updateAccount(@RequestBody UpdateAccountDTO phone) {
        return appUserService.updateAccount(phone);
    }


}
