package com.fs.company.controller.company;

import cn.hutool.core.bean.BeanUtil;
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.CompanyMoneyLogs;
import com.fs.company.domain.CompanyProfit;
import com.fs.company.domain.CompanyProfitLogs;
import com.fs.company.service.ICompanyMoneyLogsService;
import com.fs.company.service.ICompanyProfitLogsService;
import com.fs.company.service.ICompanyProfitService;
import com.fs.company.service.ICompanyService;
import com.fs.company.vo.CompanyProfitVO;
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.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 提现Controller
 *
 * @author fs
 * @date 2022-07-04
 */
@RestController
@RequestMapping("/company/companyProfit")
public class CompanyProfitController extends BaseController
{
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ICompanyProfitService companyProfitService;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private ICompanyMoneyLogsService moneyLogsService;
    @Autowired
    private ICompanyProfitLogsService logsService;
    /**
     * 查询提现列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyProfit:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyProfit companyProfit)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        companyProfit.setCompanyId(loginUser.getCompany().getCompanyId());
        List<CompanyProfitVO> list = companyProfitService.selectCompanyProfitListVO(companyProfit);
        return getDataTable(list);
    }

    /**
     * 导出提现列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyProfit:export')")
    @Log(title = "提现", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyProfit companyProfit)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        companyProfit.setCompanyId(loginUser.getCompany().getCompanyId());
        List<CompanyProfitVO> list = companyProfitService.selectCompanyProfitListVO(companyProfit);
        ExcelUtil<CompanyProfitVO> util = new ExcelUtil<CompanyProfitVO>(CompanyProfitVO.class);
        return util.exportExcel(list, "companyProfit");
    }

    /**
     * 获取提现详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:companyProfit:query')")
    @GetMapping(value = "/{profitId}")
    public AjaxResult getInfo(@PathVariable("profitId") Long profitId)
    {
        return AjaxResult.success(companyProfitService.selectCompanyProfitById(profitId));
    }

    /**
     * 新增提现
     */
    @PreAuthorize("@ss.hasPermi('company:companyProfit:add')")
    @Log(title = "提现", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompanyProfit companyProfit)
    {
        return toAjax(companyProfitService.insertCompanyProfit(companyProfit));
    }

    /**
     * 修改提现
     */
    @PreAuthorize("@ss.hasPermi('company:companyProfit:edit')")
    @Log(title = "提现", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CompanyProfit companyProfit)
    {
        return toAjax(companyProfitService.updateCompanyProfit(companyProfit));
    }

    /**
     * 删除提现
     */
    @PreAuthorize("@ss.hasPermi('company:companyProfit:remove')")
    @Log(title = "提现", businessType = BusinessType.DELETE)
	@DeleteMapping("/{profitIds}")
    public AjaxResult remove(@PathVariable Long[] profitIds)
    {
        return toAjax(companyProfitService.deleteCompanyProfitByIds(profitIds));
    }

    @PreAuthorize("@ss.hasPermi('company:companyProfit:profit')")
    @PostMapping("/profit")
    @Transactional
    @RepeatSubmit
    public R profit(@RequestBody CompanyProfit companyProfit)
    {
        if(companyProfit.getMoney().compareTo(new BigDecimal(0))<1){
            return R.error("提现金额应大于0");
        }
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Company company=companyService.selectCompanyByIdForUpdate(loginUser.getCompany().getCompanyId());
        if(companyProfit.getMoney().compareTo(company.getMoney())==1){
            return R.error("提现金额应小于等于帐户余额");
        }
        company.setMoney(company.getMoney().subtract(companyProfit.getMoney()));
        CompanyProfit profit=new CompanyProfit();
        BeanUtil.copyProperties(companyProfit,profit);
        profit.setCompanyId(company.getCompanyId());
        profit.setProfitStatus(1);
        profit.setBalances(company.getMoney());
        profit.setCreateTime(new Date());
        companyProfitService.insertCompanyProfit(profit);
        companyService.updateCompany(company);

        CompanyMoneyLogs logs=new CompanyMoneyLogs();
        logs.setCompanyId(company.getCompanyId());
        logs.setMoney(companyProfit.getMoney().multiply(new BigDecimal(-1)));
        logs.setLogsType(7);
        logs.setBalance(company.getMoney());
        logs.setRemark("提现:"+companyProfit.getMoney());
        logs.setCreateTime(new Date());
        logs.setBusinessId(profit.getProfitId().toString());
        moneyLogsService.insertCompanyMoneyLogs(logs);
        CompanyProfitLogs profitLogs=new CompanyProfitLogs();
        profitLogs.setProfitId(profit.getProfitId());
        profitLogs.setReason("申请提现");
        profitLogs.setCreateTime(new Date());
        profitLogs.setTitle("用户申请提现");
        logsService.insertCompanyProfitLogs(profitLogs);
        return R.ok("提交成功,等待后台审核");
    }
    @PreAuthorize("@ss.hasPermi('company:companyProfit:cancel')")
    @PostMapping("/cancel")
    @Transactional
    @RepeatSubmit
    public R cancel(@RequestBody CompanyProfit companyProfit)
    {
        CompanyProfit profit=companyProfitService.selectCompanyProfitById(companyProfit.getProfitId());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Company company=companyService.selectCompanyByIdForUpdate(loginUser.getCompany().getCompanyId());
        if(profit.getProfitStatus()!=1){
            return R.error("非法操作");
        }
        profit.setProfitStatus(0);
        companyProfitService.updateCompanyProfit(profit);
        company.setMoney(company.getMoney().add(profit.getMoney()));
        companyService.updateCompany(company);
        CompanyMoneyLogs logs=new CompanyMoneyLogs();
        logs.setCompanyId(company.getCompanyId());
        logs.setMoney(profit.getMoney());
        logs.setLogsType(9);
        logs.setBalance(company.getMoney());
        logs.setRemark("取消提现:"+profit.getMoney());
        logs.setCreateTime(new Date());
        logs.setBusinessId(profit.getProfitId().toString());
        moneyLogsService.insertCompanyMoneyLogs(logs);
        CompanyProfitLogs profitLogs=new CompanyProfitLogs();
        profitLogs.setProfitId(profit.getProfitId());
        profitLogs.setReason("取消提现");
        profitLogs.setCreateTime(new Date());
        profitLogs.setTitle("用户取消提现");
        logsService.insertCompanyProfitLogs(profitLogs);
        return R.ok("提交成功,等待后台审核");
    }
}
