package com.sqx.modules.app.controller.app;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.app.annotation.LoginUser;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.AppService;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.message.entity.MessageInfo;
import com.sqx.modules.message.service.MessageService;
import com.sqx.modules.resumes.entity.Company;
import com.sqx.modules.resumes.entity.Resumes;
import com.sqx.modules.resumes.service.CompanyService;
import com.sqx.modules.resumes.service.ResumesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;

/**
 * APP登录授权
 */
@RestController
@RequestMapping("/app/user")
@Api(value = "APP管理", tags = {"APP管理"})
public class AppController {

    @Autowired
    private UserService userService;
    @Autowired
    private AppService appService;
    @Autowired
    private CommonInfoService commonInfoService;
    @Autowired
    private ResumesService resumesService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private CompanyService companyService;

    @Login
    @PostMapping("/updateUserResumes")
    @ApiOperation("修改用户")
    public Result updateUserResumes(  @RequestBody UserEntity user, String resumesContent,@RequestAttribute("userId")Long userId) {
        user.setUserId(userId);
        userService.updateById(user);
        MessageInfo messageInfo = new MessageInfo();
        messageInfo.setState("10");
        messageInfo.setIsSee("0");
        messageInfo.setContent(String.valueOf(user.getUserId()));
        messageService.save(messageInfo);
        return Result.success();
    }

    @Login
    @PostMapping("/updateUserResumess")
    @ApiOperation("修改用户")
    public Result updateUserResumess(@LoginUser UserEntity user) {
        Resumes resumes = resumesService.selectResumesByUserId(user.getUserId());
        if (resumes == null) {
            return Result.error("请先添加简历！");
        }
        resumes.setUpdateTime(DateUtils.format(new Date()));
        resumesService.updateById(resumes);
        userService.updateById(user);
        return Result.success();
    }


    @RequestMapping(value = "/selectUserByIds", method = RequestMethod.GET)
    @ApiOperation("获取用户详细信息")
    @ResponseBody
    public Result selectUserByIds(Long userId) {
        return Result.success().put("data", userService.selectUserById(userId));
    }

    @Login
    @RequestMapping(value = "/updatePwd", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("用户端修改密码")
    public Result updatePwd(@LoginUser UserEntity user, String pwd, String oldPwd) {
        if (!user.getPassword().equals(DigestUtils.sha256Hex(oldPwd))) {
            return Result.error("原始密码不正确！");
        }
        if (pwd.equals(oldPwd)) {
            return Result.error("新密码不能与旧密码相同！");
        }
        user.setPassword(DigestUtils.sha256Hex(pwd));
        userService.updateById(user);
        return Result.success();
    }

    @Login
    @RequestMapping(value = "/updatePhone", method = RequestMethod.POST)
    @ApiOperation("用户端换绑手机号")
    @ResponseBody
    public Result updatePhone(@RequestAttribute("userId") Long userId, @RequestParam String phone, @RequestParam String msg) {
        return userService.updatePhone(phone, msg, userId);
    }

    @Login
    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    @ApiOperation("用户修改个人信息")
    @ResponseBody
    public Result updateUser(@RequestBody UserEntity userEntity, @RequestAttribute("userId") Long userId) {
        userEntity.setUserId(userId);
        userService.updateById(userEntity);
        return Result.success();
    }

    @Login
    @RequestMapping(value = "/updateUserImageUrl", method = RequestMethod.POST)
    @ApiOperation("用户修改头像")
    @ResponseBody
    public Result updateUserImageUrl(@LoginUser UserEntity user, String avatar) {
        user.setAvatar(avatar);
        userService.updateById(user);
        return Result.success();
    }

    @Login
    @RequestMapping(value = "/updateUserName", method = RequestMethod.POST)
    @ApiOperation("用户修改昵称")
    @ResponseBody
    public Result updateUserName(@LoginUser UserEntity user, String userName) {
        user.setUserName(userName);
        userService.updateById(user);
        return Result.success();
    }

    @Login
    @RequestMapping(value = "/selectUserById", method = RequestMethod.GET)
    @ApiOperation("获取用户详细信息")
    @ResponseBody
    public Result selectUserById(@RequestAttribute("userId")Long userId) {
//        if (user.getUserType() == 2) {
//            Company company = companyService.getOne(new QueryWrapper<Company>().eq("user_id", user.getUserId()));
//            if (company != null) {
//                user.setCompanyId(company.getCompanyId());
//            }
//        }
//        if (user.getZhiRate() == null) {
//            user.setZhiRate(new BigDecimal(commonInfoService.findOne(207).getValue()));
//        }
//        if (user.getFeiRate() == null) {
//            user.setFeiRate(new BigDecimal(commonInfoService.findOne(208).getValue()));
//        }
        UserEntity user = userService.selectUserById(userId);
        return Result.success().put("data", user);

    }


    @RequestMapping(value = "/selectNewApp", method = RequestMethod.GET)
    @ApiOperation("升级检测")
    @ResponseBody
    public Result selectNewApp() {
        return Result.success().put("data", appService.selectNewApp());
    }

    @PostMapping("updateClientId")
    @ApiOperation("绑定ClientId")
    public Result updateClientId(String clientId, Long userId) {
        UserEntity userEntity = new UserEntity();
        userEntity.setUserId(userId);
        userEntity.setClientid(clientId);
        userService.updateById(userEntity);
        return Result.success();
    }


}
