package com.fs.company.controller;

import com.fs.common.annotation.Log;
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.domain.model.LoginUser;
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.CompanyMoneyLogs;
import com.fs.company.domain.CompanyRecharge;
import com.fs.company.service.ICompanyMoneyLogsService;
import com.fs.company.service.ICompanyRechargeService;
import com.fs.company.service.ICompanyService;
import com.fs.company.vo.CompanyRechargeExportVO;
import com.fs.company.vo.CompanyRechargeVO;
import com.fs.framework.web.service.TokenService;
import lombok.Synchronized;
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.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 TokenService tokenService;
    @Autowired
    private ICompanyRechargeService companyRechargeService;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private ICompanyMoneyLogsService moneyLogsService;
    /**
     * 查询充值列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyRecharge:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyRecharge companyRecharge)
    {
        startPage();
        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)
    {
        List<CompanyRechargeExportVO> list = companyRechargeService.selectCompanyRechargeExportList(companyRecharge);
        ExcelUtil<CompanyRechargeExportVO> util = new ExcelUtil<CompanyRechargeExportVO>(CompanyRechargeExportVO.class);
        return util.exportExcel(list, "公司充值明细");
    }

    /**
     * 获取充值详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:companyRecharge:query')")
    @GetMapping(value = "/{rechargeId}")
    public AjaxResult getInfo(@PathVariable("rechargeId") Long rechargeId)
    {
        return AjaxResult.success(companyRechargeService.selectCompanyRechargeById(rechargeId));
    }

    /**
     * 新增充值
     */
    @PreAuthorize("@ss.hasPermi('company:companyRecharge:add')")
    @Log(title = "充值", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompanyRecharge companyRecharge)
    {
        return toAjax(companyRechargeService.insertCompanyRecharge(companyRecharge));
    }



    /**
     * 删除充值
     */
    @PreAuthorize("@ss.hasPermi('company:companyRecharge:remove')")
    @Log(title = "充值", businessType = BusinessType.DELETE)
	@DeleteMapping("/{rechargeIds}")
    public AjaxResult remove(@PathVariable Long[] rechargeIds)
    {
        return toAjax(companyRechargeService.deleteCompanyRechargeByIds(rechargeIds));
    }



    @PreAuthorize("@ss.hasPermi('company:companyRecharge:audit')")
    @PostMapping("/audit")
    @Transactional
    @Synchronized
    public R audit(@RequestBody CompanyRecharge param)
    {
        CompanyRecharge companyRecharge=companyRechargeService.selectCompanyRechargeById(param.getRechargeId());
        if(companyRecharge.getIsAudit()!=0){
            return R.error("非法操作");
        }
        companyRecharge.setIsAudit(param.getIsAudit());
        companyRecharge.setRemark(param.getRemark());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if(companyRecharge.getIsAudit()==1){
            Company company=companyService.selectCompanyById(companyRecharge.getCompanyId());
            company.setMoney(company.getMoney().add(companyRecharge.getMoney()));
            companyService.updateCompany(company);
            CompanyMoneyLogs log=new CompanyMoneyLogs();
            log.setCompanyId(companyRecharge.getCompanyId());
            log.setMoney(companyRecharge.getMoney());
            log.setRemark(companyRecharge.getRemark());
            log.setLogsType(1);
            log.setBalance(company.getMoney());
            log.setCreateTime(new Date());
            moneyLogsService.insertCompanyMoneyLogs(log);
            companyRecharge.setPayTime(new Date());
            companyRecharge.setStatus(1);
        }
        companyRecharge.setAuditTime(new Date());
        companyRecharge.setAuditUserId(loginUser.getUser().getUserId());
        companyRechargeService.updateCompanyRecharge(companyRecharge);
        return R.ok("操作成功");

    }
}
