package com.willalp.web.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.core.domain.entity.SysUser;
import com.willalp.common.enums.SysOrgCodeEnum;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.common.utils.StringUtils;
import com.willalp.common.utils.http.HttpUtils;
import com.willalp.files.domain.HsOrgPersonnelFiles;
import com.willalp.files.service.IHsOrgPersonnelFilesService;
import com.willalp.system.service.ISysUserService;
import io.swagger.annotations.Api;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.net.URLDecoder;


@Api("api外部接口")
@RestController
@RequestMapping("/api")
public class ApiOneController {

    @Resource
    private ISysUserService userService;

    @Resource
    private IHsOrgPersonnelFilesService orgPersonnelFilesService;

    @Resource
    private HttpUtils httpUtils;

    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/add")
    public AjaxResult add(@RequestBody SysUser user) {
        try {
            user.setNickName(
                    URLDecoder.decode(user.getNickName(), "utf8"));
            user.setCreateBy("app");
            user.setUserType("1");
            user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
            HsOrgPersonnelFiles opf = new HsOrgPersonnelFiles();
            opf.setUserName(URLDecoder.decode(user.getNickName(), "utf8"));
            opf.setUserCode(user.getUserName());
            opf.setPhone(user.getPhonenumber());
            opf.setOrgCode(SysOrgCodeEnum.MLHuiZhuLi.getCode());
            orgPersonnelFilesService.save(opf);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
        return AjaxResult.success(userService.insertUser(user));
    }


    /**
     * 重置密码
     *
     * @param un
     * @return
     */
    @GetMapping("/updatePwd")
    public AjaxResult resetPwd(@RequestParam String un, @RequestParam String pwd) {
        if (StringUtils.isNotBlank(un)) {
            int result = 0;
            if (StringUtils.isBlank(pwd)) {
                result = userService.resetUserPwd(un, SecurityUtils.encryptPassword("123456"));
            } else {
                result = userService.resetUserPwd(un, SecurityUtils.encryptPassword(pwd));
            }
            if (result > 0) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error();
            }
        } else {
            return AjaxResult.error("用户编码为空");
        }
    }

    /**
     * 获取主服务机构
     *
     * @return
     */
    @GetMapping("/getOrganization")
    public AjaxResult getOrganization() {
        String result = httpUtils.sendSSLPost(httpUtils.getHttpsMasterUrl("/getOrganization"), "");
        return AjaxResult.success(JSONObject.parseObject(result));
    }
}
