package com.dgtly.web.controller.people;

import com.dgtly.common.annotation.Log;
import com.dgtly.common.constant.UserConstants;
import com.dgtly.common.core.controller.BaseController;
import com.dgtly.common.core.domain.AjaxResult;
import com.dgtly.common.core.page.TableDataInfo;
import com.dgtly.common.enums.BusinessType;
import com.dgtly.common.utils.security.EncryptPassWordClass;
import com.dgtly.framework.util.ShiroUtils;
import com.dgtly.system.domain.Community;
import com.dgtly.system.domain.SysUser;
import com.dgtly.system.service.ICommunityService;
import com.dgtly.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/people/people")
public class PeopleController extends BaseController {
    private String prefix = "system/people";

    @Autowired
    private ISysUserService userService;
    @Autowired
    private ICommunityService communityService;

    @RequiresPermissions("people:people:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/people";
    }

    @RequiresPermissions("people:people:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysUser user)
    {
        startPage();
        List<SysUser> list = userService.selectPeople(user);
        return getDataTable(list);
    }

    /**
     * 新增普通用户
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("communitys", communityService.selectCommunityList(new Community()));
        return prefix + "/add";
    }
    /**
     * 新增保存用户
     */
    @RequiresPermissions("grid:people:add")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @Transactional
    public AjaxResult addSave(@Validated SysUser user)
    {
        Long[] i = {new Long(3)};
        user.setRoleIds(i);
        user.setLoginName(user.getPhonenumber());
        user.setPassword(user.getPhonenumber());
        SysUser localUser = ShiroUtils.getSysUser();
        user.setCompanyId(localUser.getCompanyId());
        if (UserConstants.USER_NAME_NOT_UNIQUE.equals(user.getLoginName()))
        {
            return error("新增用户'" + user.getLoginName() + "'失败，登录账号已存在");
        }
        else if (UserConstants.USER_PHONE_NOT_UNIQUE.equals(userService.checkPhoneUnique(user)))
        {
            return error("新增用户'" + user.getLoginName() + "'失败，手机号码已存在");
        }

        user.setSalt(ShiroUtils.randomSalt());
        user.setPassword(EncryptPassWordClass.encryptPassword(user.getLoginName(), user.getPassword(), user.getSalt()));
        user.setCreateBy(ShiroUtils.getLoginName());
        int userId = userService.insertUser(user);


        return toAjax(userId);
    }

    /**
     * 修改用户
     */
    @GetMapping("/edit/{userId}")
    public String edit(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        mmap.put("communitys", communityService.selectCommunityList(new Community()));
        mmap.put("user", userService.selectUserById(userId));
        return prefix + "/edit";
    }

}
