package com.ruoyi.web.bst;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
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.bst.areaJoin.domain.enums.AreaJoinPermission;
import com.ruoyi.bst.creditUser.domain.CreditUser;
import com.ruoyi.bst.creditUser.domain.CreditUserQuery;
import com.ruoyi.bst.creditUser.domain.CreditUserVO;
import com.ruoyi.bst.creditUser.service.CreditUserConverter;
import com.ruoyi.bst.creditUser.service.CreditUserService;
import com.ruoyi.bst.creditUser.service.CreditUserValidator;
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.core.validate.ValidGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 挂账用户Controller
 *
 * @author ruoyi
 * @date 2025-05-28
 */
@RestController
@RequestMapping("/bst/creditUser")
public class CreditUserController extends BaseController
{
    @Autowired
    private CreditUserService creditUserService;

    @Autowired
    private CreditUserConverter creditUserConverter;

    @Autowired
    private CreditUserValidator creditUserValidator;

    /**
     * 查询挂账用户列表
     */
    @PreAuthorize("@ss.hasPermi('bst:creditUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(CreditUserQuery query)
    {
        startPage();
        startOrderBy();
        query.setScope(true);
        query.addAreaPermission(AreaJoinPermission.CREDIT_USER_VIEW.getCode());
        List<CreditUserVO> list = creditUserService.selectCreditUserList(query);
        return getDataTable(list);
    }

    /**
     * 导出挂账用户列表
     */
    @PreAuthorize("@ss.hasPermi('bst:creditUser:export')")
    @Log(title = "挂账用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CreditUserQuery query)
    {
        query.setScope(true);
        query.addAreaPermission(AreaJoinPermission.CREDIT_USER_VIEW.getCode());
        List<CreditUserVO> list = creditUserService.selectCreditUserList(query);
        ExcelUtil<CreditUserVO> util = new ExcelUtil<CreditUserVO>(CreditUserVO.class);
        util.exportExcel(response, list, "挂账用户数据");
    }

    /**
     * 获取挂账用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('bst:creditUser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(creditUserService.selectCreditUserById(id, true));
    }

    /**
     * 新增挂账用户
     */
    @PreAuthorize("@ss.hasPermi('bst:creditUser:add')")
    @Log(title = "挂账用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.Create.class) CreditUserVO creditUser)
    {
        CreditUser po = creditUserConverter.toPoByCreate(creditUser);
        return toAjax(creditUserService.insertCreditUser(po));
    }

    /**
     * 修改挂账用户
     */
    @PreAuthorize("@ss.hasPermi('bst:creditUser:edit')")
    @Log(title = "挂账用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated(ValidGroup.Update.class) CreditUserVO creditUser)
    {
        ServiceUtil.assertion(!creditUserValidator.hasPermission(creditUser.getId(), AreaJoinPermission.CREDIT_USER_EDIT), 
        "您无权操作ID为%s的挂账用户", creditUser.getId());
        CreditUser po = creditUserConverter.toPoByCreate(creditUser);
        return toAjax(creditUserService.updateCreditUser(po));
    }

    /**
     * 删除挂账用户
     */
    @PreAuthorize("@ss.hasPermi('bst:creditUser:remove')")
    @Log(title = "挂账用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        ServiceUtil.assertion(!creditUserValidator.hasPermission(ids, AreaJoinPermission.CREDIT_USER_EDIT), 
        "您无权操作ID为%s的挂账用户", ids);
        return toAjax(creditUserService.deleteCreditUserByIds(ids));
    }
}
