package com.fs.company.controller.company;

import com.fs.common.annotation.Log;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.StringUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.domain.Company;
import com.fs.company.domain.CompanyRecharge;
import com.fs.company.domain.CompanyRedPacketBalanceLogs;
import com.fs.company.param.CompanyRechargeParam;
import com.fs.company.service.ICompanyRechargeService;
import com.fs.company.service.ICompanyRedPacketBalanceLogsService;
import com.fs.company.service.impl.CompanyServiceImpl;
import com.fs.core.utils.OrderCodeUtils;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/company/companyRedPacketBalanceLogs")
public class CompanyRedPacketBalanceLogsController extends BaseController {

    @Autowired
    private ICompanyRedPacketBalanceLogsService companyRedPacketBalanceLogsService;
    @Autowired
    private CompanyServiceImpl companyService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ICompanyRechargeService rechargeService;

    /**
     * 查询企业红包余额记录列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyRedPacketBalanceLogs:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyRedPacketBalanceLogs companyRedPacketBalanceLogs)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        companyRedPacketBalanceLogs.setCompanyId(loginUser.getCompany().getCompanyId());
        List<CompanyRedPacketBalanceLogs> list = companyRedPacketBalanceLogsService.selectCompanyRedPacketBalanceLogsList(companyRedPacketBalanceLogs);
        return getDataTable(list);
    }

    /**
     * 导出企业红包余额记录列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyRedPacketBalanceLogs:export')")
    @Log(title = "企业红包余额记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyRedPacketBalanceLogs companyRedPacketBalanceLogs)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        companyRedPacketBalanceLogs.setCompanyId(loginUser.getCompany().getCompanyId());
        List<CompanyRedPacketBalanceLogs> list = companyRedPacketBalanceLogsService.selectCompanyRedPacketBalanceLogsList(companyRedPacketBalanceLogs);
        ExcelUtil<CompanyRedPacketBalanceLogs> util = new ExcelUtil<CompanyRedPacketBalanceLogs>(CompanyRedPacketBalanceLogs.class);
        return util.exportExcel(list, "企业红包余额记录数据");
    }

    /**
     * 点击红包充值按钮，获取企业红包余额等信息
     */
    @GetMapping("/redBalance")
    public R getCompanyRedPacketBalance(){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Company  company=companyService.selectCompanyById(loginUser.getCompany().getCompanyId());
        return R.ok().put("data",company);
    }

    /**
     * @Description: 红包充值功能 因公司余额的变动关联的地方太多，现在把充值红包的金额单独提出来 不合计到公司余额中
     * @Param:
     * @Return:
     * @Author xgb
     * @Date 2025/11/3 11:08
     */
    @PreAuthorize("@ss.hasPermi('company:companyRecharge:Recharge')")
    @Log(title = "红包充值", businessType = BusinessType.INSERT)
    @PostMapping(value = "/redRecharge")
    @Transactional
    @RepeatSubmit
    public R redRecharge(@RequestBody CompanyRechargeParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        CompanyRecharge redRecharge=new CompanyRecharge();
        String orderSn =  OrderCodeUtils.getOrderSn();
        if(StringUtils.isEmpty(orderSn)){
            return R.error("订单生成失败，请重试");
        }
        redRecharge.setRechargeNo(orderSn);
        redRecharge.setCompanyId(param.getCompanyId());
        redRecharge.setMoney(param.getMoney());
        redRecharge.setCreateUserId(loginUser.getUser().getUserId());
        redRecharge.setIsAudit(0);
        redRecharge.setStatus(1);
        redRecharge.setRemark(param.getRemark());
        redRecharge.setPayType(3);
        redRecharge.setBusinessType(1);// 红包充值
        redRecharge.setImgs(param.getImgs());// 红包充值
        rechargeService.insertCompanyRecharge(redRecharge);
        return R.ok("提交成功,等待审核");

    }

}
