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

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.PostMapping;
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.MerchantEmployeeQuery;
import com.ruoyi.web.modules.account.dto.MerchantEmployeeCreateDTO;
import com.ruoyi.web.modules.account.dto.MerchantEmployeePasswordDTO;
import com.ruoyi.web.modules.account.dto.MerchantEmployeeProfileDTO;
import com.ruoyi.web.modules.account.dto.MerchantEmployeeUpdateDTO;
import com.ruoyi.web.modules.account.service.IMerchantEmployeeService;

/**
 * 商家端员工管理
 */
@RestController
@RequestMapping("/agri/seller/employee")
public class MerchantEmployeeController extends BaseController
{
    @Autowired
    private IMerchantEmployeeService merchantEmployeeService;

    @PreAuthorize("@ss.hasPermi('agri:seller:employee:list')")
    @GetMapping("/list")
    public TableDataInfo list(MerchantEmployeeQuery query)
    {
        if (query == null)
        {
            query = new MerchantEmployeeQuery();
        }
        merchantEmployeeService.fillListScope(query, getUserId());
        startPage();
        return getDataTable(merchantEmployeeService.selectEmployeeList(query));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:employee:list')")
    @GetMapping("/quota")
    public AjaxResult quota()
    {
        return success(merchantEmployeeService.selectQuota(getUserId()));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:employee:list')")
    @GetMapping("/roleOptions")
    public AjaxResult roleOptions()
    {
        return success(merchantEmployeeService.selectRoleOptions(getUserId()));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:employee:query')")
    @GetMapping("/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return success(merchantEmployeeService.selectEmployeeDetail(userId, getUserId()));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:employee:add')")
    @Log(title = "员工管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MerchantEmployeeCreateDTO dto)
    {
        Long userId = merchantEmployeeService.createEmployee(dto, getUserId(), getUsername());
        AjaxResult result = success();
        result.put("userId", userId);
        return result;
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:employee:edit')")
    @Log(title = "员工管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MerchantEmployeeUpdateDTO dto)
    {
        merchantEmployeeService.updateEmployee(dto, getUserId(), getUsername());
        return success();
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:employee:edit')")
    @Log(title = "员工管理", businessType = BusinessType.UPDATE)
    @PutMapping("/profile")
    public AjaxResult editProfile(@RequestBody MerchantEmployeeProfileDTO dto)
    {
        merchantEmployeeService.updateProfile(dto, getUserId(), getUsername());
        return success();
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:employee:resetPwd')")
    @Log(title = "员工管理", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody MerchantEmployeePasswordDTO dto)
    {
        merchantEmployeeService.updatePassword(dto, getUserId(), getUsername());
        return success();
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:employee:remove')")
    @Log(title = "员工管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable("userIds") Long[] userIds)
    {
        merchantEmployeeService.deleteEmployees(userIds, getUserId(), getUsername());
        return success();
    }
}
