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.poi.ExcelUtil;
import com.fs.company.domain.Company;
import com.fs.company.domain.CompanyRecharge;
import com.fs.company.service.ICompanyRechargeService;
import com.fs.company.service.ICompanyService;
import com.fs.company.util.OrderUtils;
import com.fs.company.vo.CompanyRechargeVO;
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.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 充值Controller
 *
 * @author fs
 * @date 2021-10-04
 */
@RestController
@RequestMapping("/company/companyRecharge")
public class CompanyRechargeController extends BaseController
{
    @Autowired
    private ICompanyRechargeService companyRechargeService;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询充值列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyRecharge:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyRecharge companyRecharge)
    {
        startPage();
        companyRecharge.setStatus(1);
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        companyRecharge.setCompanyId(loginUser.getCompany().getCompanyId());
        List<CompanyRechargeVO> list = companyRechargeService.selectCompanyRechargeVOList(companyRecharge);
        return getDataTable(list);
    }

    /**
     * 导出充值列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyRecharge:export')")
    @Log(title = "充值", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyRecharge companyRecharge)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        companyRecharge.setCompanyId(loginUser.getCompany().getCompanyId());
        List<CompanyRecharge> list = companyRechargeService.selectCompanyRechargeList(companyRecharge);
        ExcelUtil<CompanyRecharge> util = new ExcelUtil<CompanyRecharge>(CompanyRecharge.class);
        return util.exportExcel(list, "companyRecharge");
    }



    /**
     * 新增充值
     */
    @PreAuthorize("@ss.hasPermi('company:companyRecharge:add')")
    @PostMapping
    @RepeatSubmit
    public R add(@RequestBody CompanyRecharge companyRecharge)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        companyRecharge.setCompanyId(loginUser.getCompany().getCompanyId());
        companyRecharge.setRechargeNo(OrderUtils.getOrderNo());
        companyRecharge.setCreateTime(new Date());
        companyRecharge.setStatus(0);
        if(companyRecharge.getMoney().compareTo(new BigDecimal(0))<1){
            return R.error("充值金额必须大于0");
        }
        else
        {
            Company company=companyService.selectCompanyById(loginUser.getCompany().getCompanyId());
            companyRecharge.setBalance(company.getMoney());
            if(companyRechargeService.insertCompanyRecharge(companyRecharge)>0){
                return R.ok().put("rechargeNo",companyRecharge.getRechargeNo());
            }
            else {
                return R.error("操作失败");
            }
        }
    }


}
