package com.ruoyi.web.modules.account.controller;

import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.modules.account.domain.AccountManageQuery;
import com.ruoyi.web.modules.account.dto.AccountPasswordUpdateDTO;
import com.ruoyi.web.modules.account.dto.AccountProfileUpdateDTO;
import com.ruoyi.web.modules.account.service.IAccountManageService;

/**
 * 账号管理
 */
@RestController
@RequestMapping("/agri/accountManage")
public class AccountManageController extends BaseController
{
    @Autowired
    private IAccountManageService accountManageService;

    @PreAuthorize("@ss.hasPermi('agri:accountManage:list')")
    @GetMapping("/list")
    public TableDataInfo list(AccountManageQuery query)
    {
        startPage();
        return getDataTable(accountManageService.selectAccountList(query));
    }

    @PreAuthorize("@ss.hasPermi('agri:accountManage:query')")
    @GetMapping("/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return success(accountManageService.selectAccountDetail(userId));
    }

    @PreAuthorize("@ss.hasPermi('agri:accountManage:edit')")
    @Log(title = "账号管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AccountProfileUpdateDTO dto)
    {
        Map<String, Object> data = accountManageService.updateProfile(dto, getUsername());
        if (Boolean.TRUE.equals(data.get("needConfirm")))
        {
            AjaxResult result = AjaxResult.success();
            result.put(AjaxResult.DATA_TAG, data);
            return result;
        }
        return success();
    }

    @PreAuthorize("@ss.hasPermi('agri:accountManage:resetPwd')")
    @Log(title = "账号管理", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody AccountPasswordUpdateDTO dto)
    {
        accountManageService.updatePassword(dto, getUsername());
        return success();
    }

    @PreAuthorize("@ss.hasPermi('agri:accountManage:remove')")
    @Log(title = "账号管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable("userIds") Long[] userIds)
    {
        accountManageService.deleteAccounts(userIds, getUserId(), getUsername());
        return success();
    }
}
