package com.boco.nbd.wios.manage.controller;

import cn.hutool.core.lang.Assert;
import com.boco.nbd.wios.flow.util.ProcessUtil;
import com.boco.nbd.wios.manage.contants.WiosConstant;
import com.boco.nbd.wios.manage.entity.bo.AccountBo;
import com.boco.nbd.wios.manage.entity.bo.AccountVo;
import com.boco.nbd.wios.manage.entity.bo.Oem;
import com.boco.nbd.wios.manage.entity.bo.Role;
import com.boco.nbd.wios.manage.mapper.def.OemDao;
import com.boco.nbd.wios.manage.service.impl.AccountService;
import com.boco.nbd.wios.manage.service.impl.RoleService;
import com.ihidea.component.api.v2.BaseResponse;
import com.ihidea.core.support.exception.ServiceException;
import com.ihidea.core.support.session.SessionInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * 账户相关api
 *
 * @author lilin
 * @version [版本号, 2020年5月9日]
 */
@RestController
@RequestMapping("api")
@Api(tags = "账户管理接口")
@ApiIgnore
public class AccountController {
    /**
     * 账户service
     */
    @Resource
    private AccountService accountService;

    /**
     * 角色service
     */
    @Resource
    private RoleService roleService;

    @Autowired
    private OemDao oemDao;


    /**
     * 查询账户列表
     *
     * @param name
     * @param status
     * @param type
     * @param account
     * @return
     */
    @GetMapping(value = "account/oem/list")
    @ApiOperation(value = "查询主机厂/经销商账户列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "姓名", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "oemId", value = "主机厂/经销商id", dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "type", value = "类型 1:主机厂 2:经销商", dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "account", value = "登陆账号", dataType = "String", paramType = "query", required = false),
    })
    public BaseResponse<List<AccountVo>> getOemAccountList(String name, Integer status, Integer type, String account) {
        AccountBo query = new AccountBo();
        query.setName(name);
        query.setStatus(status);
        query.setType(type);
        query.setAccount(account);
        return new BaseResponse<>(accountService.getOemAccountList(query));
    }

    /**
     * 添加主机厂/经销商账户
     *
     * @param accountInfo 账户信息
     * @return 新增成功的条数
     */
    @PostMapping(value = "account/oem/add")
    @ApiOperation(value = "添加主机厂/经销商账户")
    public BaseResponse<Integer> add(AccountBo accountInfo) {
        if (StringUtils.isEmpty(accountInfo.getRoleIds())) {
            throw new ServiceException("缺少角色");
        }
        SessionInfo currentUser = ProcessUtil.getUserInfo();
        if (currentUser == null) {
            return new BaseResponse<>(0);
        }
        if (WiosConstant.SUPER_USER_ID.equals(currentUser.getUserId())) {
            if (accountInfo.getOemId() == null) {
                throw new ServiceException("缺少主机厂/经销商id");
            }
            Oem oem = oemDao.selectById(accountInfo.getOemId());
            Integer parentId = oem.getWbAccountId();
            accountInfo.setParentId(parentId);
        } else {
            accountInfo.setParentId(Integer.valueOf(currentUser.getUserId()));
        }
        accountInfo.setPassword(WiosConstant.ACCOUNT_INIT_PASSWORD);
        return new BaseResponse<>(accountService.addAccount(accountInfo));
    }


    /**
     * 修改账户
     *
     * @param accountInfo 账户信息
     * @return 修改成功的数量
     */
    @PostMapping(value = "account/update")
    @ApiOperation(value = "修改账户")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "name", value = "账户名称", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "account", value = "登陆账号", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "roleIds", value = "角色id", dataType = "String", paramType = "query", required = false),
    })
    public BaseResponse<Integer> update(AccountBo accountInfo) {
        return new BaseResponse<>(accountService.updateAccount(accountInfo));
    }

    /**
     * 修改密码
     *
     * @param id          账户id
     * @param oldPassword 旧密码
     * @param password    新密码
     * @return 是否修改成功:1成功,其他失败
     */
    @PostMapping(value = "account/updatePassword")
    @ApiOperation(value = "修改密码")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "oldPassword", value = "老密码", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "password", value = "新密码", dataType = "String", paramType = "query", required = true),})
    public BaseResponse<Integer> updatePassword(Integer id, String oldPassword, String password) {
        return new BaseResponse<>(accountService.updatePassword(id, oldPassword, password));
    }

    /**
     * 重置密码
     *
     * @param id
     * @return
     */
    @PostMapping(value = "account/initPassword")
    @ApiOperation(value = "重置密码")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键", dataType = "int", paramType = "query", required = true)})
    public BaseResponse<Integer> updatePassword(Integer id) {
        return new BaseResponse<>(accountService.initPassword(id));
    }

    /**
     * 发送短信验证码
     *
     * @param phone
     * @return
     */
    @PostMapping(value = "account/sendCaptcha")
    @ApiOperation(value = "发送短信验证码")
    @ApiImplicitParams({@ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true)})
    public BaseResponse<Object> updatePasswodByCaptcha(String phone) {
        Assert.notEmpty(phone, "phone不能为空");
        accountService.sendCaptcha(phone);
        return new BaseResponse<>();
    }

    /**
     * 通过短信验证码修改密码
     *
     * @param newPassword
     * @param captcha
     * @param phone
     * @return
     */
    @PostMapping(value = "account/updatePasswodByCaptcha")
    @ApiOperation(value = "通过短信验证码修改密码")
    @ApiImplicitParams({@ApiImplicitParam(name = "newPassword", value = "新密码", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "captcha", value = "验证码", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true)})
    public BaseResponse<Object> updatePasswodByCaptcha(String newPassword, String captcha, String phone) {
        Assert.notEmpty(newPassword, "newPassword不能为空");
        Assert.notEmpty(captcha, "captcha不能为空");
        Assert.notEmpty(phone, "phone不能为空");
        accountService.resetPwd(newPassword, captcha, phone);
        return new BaseResponse<>();
    }

    /**
     * 根据账户id查询角色列表
     *
     * @param accountId 账户id
     * @return 角色列表
     */
    @GetMapping(value = "account/roleList")
    @ApiOperation(value = "根据账户id查询角色列表")
    @ApiImplicitParam(name = "accountId", value = "账户id", dataType = "int", paramType = "query", required = true)
    public BaseResponse<List<Role>> findAccountRoleList(String accountId) {
        return new BaseResponse<>(roleService.findAccountRoleList(accountId));
    }
}