package com.ruoyi.web.controller.buyerapi;

import java.util.List;

import cn.hutool.core.util.IdUtil;

import javax.servlet.http.HttpServletResponse;

import com.ruoyi.buyerapi.domain.BuyerDeptDevice;
import com.ruoyi.buyerapi.mapper.BuyerUserMapper;
import com.ruoyi.buyerapi.service.IBuyerUserService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
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.buyerapi.domain.BuyerCompanyInfo;
import com.ruoyi.buyerapi.service.IBuyerCompanyInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员企业信息Controller
 *
 * @author zq
 * @date 2022-09-20
 */
@Api(tags = "会员企业信息")
@RestController
@RequestMapping("/buyer/company")
public class BuyerCompanyInfoController extends BaseController {
    @Autowired
    private IBuyerCompanyInfoService buyerCompanyInfoService;

    @Autowired
    private BuyerUserMapper buyerUserMapper;

    /**
     * 查询企业会员是否存在
     */
    @ApiOperation(value = "查询企业会员是否存在")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phoneNumber", value = "电话号码", required = true, paramType = "query"),
    })
    @GetMapping("/check")
    public AjaxResult check(BuyerCompanyInfo buyerCompanyInfo) {
        AjaxResult ajaxResult = new AjaxResult();
        SysUser sysUser = new SysUser();
        sysUser.setPhonenumber(buyerCompanyInfo.getPhoneNumber());
        List<SysUser> list = buyerUserMapper.selectUserList(sysUser);
        if (list.size() > 0) {
            ajaxResult.put("code", 200);
            ajaxResult.put("repeat", true);
            ajaxResult.put("msg", "账号已存在，请修改");
            return ajaxResult;
        } else {
            ajaxResult.put("code", 200);
            ajaxResult.put("repeat", false);
            return ajaxResult;
        }
    }

    /**
     * 查询会员企业信息列表
     */
    @ApiOperation(value = "查询会员企业信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "账号", required = true, paramType = "query"),
            @ApiImplicitParam(name = "phoneNumber", value = "注册电话", required = true, paramType = "query"),
            @ApiImplicitParam(name = "name", value = "认证企业名称", required = true, paramType = "query"),
            @ApiImplicitParam(name = "code", value = "信用代码", required = true, paramType = "query"),
            @ApiImplicitParam(name = "contacts", value = "联系人", required = true, paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "联系电话", required = true, paramType = "query"),
            @ApiImplicitParam(name = "accountStatus", value = "账号状态（0正常 1停用）", required = true, paramType = "query"),
            @ApiImplicitParam(name = "type", value = "会员类型（0会员 1非会员）", required = true, paramType = "query"),
            @ApiImplicitParam(name = "reviewer", value = "审核人", required = true, paramType = "query"),
            @ApiImplicitParam(name = "authStatus", value = "审核状态（0未认证 1审核中 2已认证）", required = true, paramType = "query"),
    })
    @PreAuthorize("@ss.hasPermi('buyer:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(BuyerCompanyInfo buyerCompanyInfo) {
        startPage();
        List<BuyerCompanyInfo> list = buyerCompanyInfoService.selectBuyerCompanyInfoList(buyerCompanyInfo);
        return getDataTable(list);
    }

    /**
     * 导出会员企业信息列表
     */
    @ApiOperation(value = "导出会员企业信息列表")
    @PreAuthorize("@ss.hasPermi('buyer:info:export')")
    @Log(title = "会员企业信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BuyerCompanyInfo buyerCompanyInfo) {
        List<BuyerCompanyInfo> list = buyerCompanyInfoService.selectBuyerCompanyInfoList(buyerCompanyInfo);
        ExcelUtil<BuyerCompanyInfo> util = new ExcelUtil<BuyerCompanyInfo>(BuyerCompanyInfo.class);
        util.exportExcel(response, list, "会员企业信息数据");
    }

    /**
     * 获取会员企业信息详细信息
     */
    @ApiOperation(value = "获取会员企业信息详细信息")
    @PreAuthorize("@ss.hasPermi('buyer:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(buyerCompanyInfoService.selectBuyerCompanyInfoById(id));
    }

    /**
     * 新增会员企业信息
     */
    @ApiOperation(value = "新增会员企业信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "账号", required = true, paramType = "query")
    })
    @Log(title = "会员企业信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BuyerCompanyInfo buyerCompanyInfo) {
        buyerCompanyInfo.setId(IdUtil.simpleUUID());
        buyerCompanyInfo.setCreateBy(getUsername());
        return toAjax(buyerCompanyInfoService.insertBuyerCompanyInfo(buyerCompanyInfo));
    }

    /**
     * 修改会员企业信息
     */
    @ApiOperation(value = "修改会员企业信息")
    @PreAuthorize("@ss.hasPermi('buyer:info:edit')")
    @Log(title = "会员企业信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BuyerCompanyInfo buyerCompanyInfo) {
        buyerCompanyInfo.setUpdateBy(getUsername());
        return toAjax(buyerCompanyInfoService.updateBuyerCompanyInfo(buyerCompanyInfo));
    }

    /**
     * 充值会员企业信息
     */
    @ApiOperation(value = "充值会员企业信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "expireTime", value = "到期时间", required = true, paramType = "query"),
            @ApiImplicitParam(name = "length", value = "充值时长", required = true, paramType = "query"),
            @ApiImplicitParam(name = "unit", value = "单位", required = true, paramType = "query")
    })
    @PutMapping("/recharge")
    public AjaxResult Recharge(@RequestBody BuyerCompanyInfo buyerCompanyInfo) {
        buyerCompanyInfo.setUpdateBy(getUsername());
        return toAjax(buyerCompanyInfoService.RechargeBuyerCompanyInfo(buyerCompanyInfo));
    }

    /**
     * 删除会员企业信息
     */
    @ApiOperation(value = "删除会员企业信息")
    @PreAuthorize("@ss.hasPermi('buyer:info:remove')")
    @Log(title = "会员企业信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(buyerCompanyInfoService.deleteBuyerCompanyInfoByIds(ids));
    }
}
