package com.ruoyi.web.controller.business;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
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.system.domain.CreditUser;
import com.ruoyi.system.service.ICreditUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 信用人员Controller
 * 
 * @author boman
 * @date 2022-06-10
 */
@RestController
@RequestMapping("/system/creditUser")
public class CreditUserController extends BaseController
{
    @Autowired
    private ICreditUserService creditUserService;

    /**
     * 查询信用人员列表
     */
    @PreAuthorize("@ss.hasPermi('system:creditUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(CreditUser creditUser)
    {
        startPage();
        List<CreditUser> list = creditUserService.selectCreditUserList(creditUser);
        return getDataTable(list);
    }

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

    /**
     * 新增信用人员
     */
    @PreAuthorize("@ss.hasPermi('system:creditUser:add')")
    @Log(title = "信用人员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CreditUser creditUser)
    {
        return toAjax(creditUserService.insertCreditUser(creditUser));
    }

    /**
     * 修改信用人员
     */
    @PreAuthorize("@ss.hasPermi('system:creditUser:edit')")
    @Log(title = "信用人员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CreditUser creditUser)
    {
        return toAjax(creditUserService.updateCreditUser(creditUser));
    }

    /**
     * 删除信用人员
     */
    @PreAuthorize("@ss.hasPermi('system:creditUser:remove')")
    @Log(title = "信用人员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(creditUserService.deleteCreditUserByIds(ids));
    }

    /***
     * 信用平台匹配用户信息（导入）
     */
    @Log(title = "人员信息", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:creditUser:importCredit')")
    @PostMapping("/importCredit")
    public AjaxResult importCredit(MultipartFile file) throws Exception {

        ExcelUtil<CreditUser> util = new ExcelUtil<CreditUser>(CreditUser.class);

        List<CreditUser> creditUserList = util.importExcel(file.getInputStream());

        String message = creditUserService.importCredit(creditUserList);
        return AjaxResult.success(message);
    }


    /**
     * 导出信用人员列表
     */
    @PreAuthorize("@ss.hasPermi('system:creditUser:export')")
    @Log(title = "信用人员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CreditUser creditUser)
    {
        List<CreditUser> creditUserList = creditUserService.getAllCreditUser(creditUser);
        ExcelUtil<CreditUser> util = new ExcelUtil<>(CreditUser.class);
        util.exportExcel(response, creditUserList, "信用人员数据");
    }


}
