package com.ruoyi.web.controller.ss;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.ValidGroup;
import com.ruoyi.ss.user.domain.SmUserQuery;
import com.ruoyi.ss.user.domain.SmUserVO;
import com.ruoyi.ss.user.service.UserAssembler;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.enums.BusinessType;
import com.ruoyi.common.core.domain.entity.SmUser;
import com.ruoyi.ss.user.service.ISmUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 普通用户信息Controller
 *
 * @author qzz
 * @date 2024-01-24
 */
@RestController
@RequestMapping("/system/smUser")
public class SmUserController extends BaseController
{
    @Autowired
    private ISmUserService smUserService;

    @Autowired
    private UserAssembler userAssembler;

    /**
     * 查询普通用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:smUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(SmUserQuery smUser)
    {
        startPage();
        startOrderBy();
        List<SmUserVO> list = smUserService.selectSmUserList(smUser);
        userAssembler.assembleStoreCount(list);
        userAssembler.assembleDeviceCount(list);
        userAssembler.assembleRealServiceRate(list);
        smUserService.desensitization(list);
        userAssembler.assembleWaitBonusAmount(list);
        return getDataTable(list);
    }

    /**
     * ids查询普通用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:smUser:list')")
    @GetMapping("/listByIds/{ids}")
    public AjaxResult listByIds(@PathVariable Long[] ids)
    {
        List<SmUserVO> list = smUserService.selectByIds(Arrays.asList(ids));
        return success(list);
    }

    /**
     * 导出普通用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:smUser:export')")
    @Log(title = "普通用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SmUserQuery smUser)
    {
        List<SmUserVO> list = smUserService.selectSmUserList(smUser);
        ExcelUtil<SmUserVO> util = new ExcelUtil<SmUserVO>(SmUserVO.class);
        util.exportExcel(response, list, "普通用户信息数据");
    }

    /**
     * 获取普通用户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:smUser:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        SmUserVO user = smUserService.selectSmUserByUserId(userId);
        List<SmUserVO> list = Collections.singletonList(user);
        userAssembler.assembleStoreCount(list);
        userAssembler.assembleDeviceCount(list);
        userAssembler.assembleRealServiceRate(list);
        smUserService.desensitization(list);
        userAssembler.assembleWaitBonusAmount(list);
        return success(user);
    }

    /**
     * 新增普通用户信息
     */
    @PreAuthorize("@ss.hasPermi('system:smUser:add')")
    @Log(title = "普通用户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.Create.class) SmUser smUser)
    {
        return toAjax(smUserService.insertSmUser(smUser));
    }

    /**
     * 修改普通用户信息
     */
    @PreAuthorize("@ss.hasPermi('system:smUser:edit')")
    @Log(title = "普通用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated(ValidGroup.Update.class) SmUser smUser)
    {
        return toAjax(smUserService.updateSmUser(smUser));
    }

    @ApiOperation("重置用户服务费")
    @PreAuthorize("@ss.hasPermi('system:smUser:edit')")
    @Log(title = "普通用户信息", businessType = BusinessType.UPDATE)
    @PutMapping("/{userId}/resetService")
    public AjaxResult resetService(@PathVariable Long userId)
    {
        return toAjax(smUserService.resetService(userId));
    }

    /**
     * 删除普通用户信息
     */
    @PreAuthorize("@ss.hasPermi('system:smUser:remove')")
    @Log(title = "普通用户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable List<Long> userIds) {
        return toAjax(smUserService.logicDel(userIds));
    }

    /**
     * 删除普通用户信息
     */
    @PreAuthorize("@ss.hasPermi('system:smUser:unbindRealName')")
    @Log(title = "解除实名认证", businessType = BusinessType.OTHER)
	@PutMapping("/{userId}/resetRealName")
    public AjaxResult resetRealName(@PathVariable Long userId) {
        return toAjax(smUserService.resetRealName(userId));
    }

}
