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.CompanyProfit;
import com.fs.company.domain.CompanyProfitLogs;
import com.fs.company.param.CompanyProfitAuditParam;
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.web.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.Date;
import java.util.List;

/**
 * 提现Controller
 *
 * @author fs
 * @date 2022-07-04
 */
@RestController
@RequestMapping("/company/companyProfit")
public class CompanyProfitController extends BaseController
{
    @Autowired
    private ICompanyProfitService companyProfitService;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private ICompanyMoneyLogsService moneyLogsService;
    @Autowired
    private ICompanyProfitLogsService logsService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询提现列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyProfit:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyProfit companyProfit)
    {
        startPage();
        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)
    {
        List<CompanyProfitVO> list = companyProfitService.selectCompanyProfitListVO(companyProfit);
        ExcelUtil<CompanyProfitVO> util = new ExcelUtil<CompanyProfitVO>(CompanyProfitVO.class);
        return util.exportExcel(list, "公司提现记录");
    }

    /**
     * 获取提现详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:companyProfit:query')")
    @GetMapping(value = "/{profitId}")
    public R getInfo(@PathVariable("profitId") Long profitId)
    {
        CompanyProfit profit=companyProfitService.selectCompanyProfitById(profitId);
        CompanyProfitLogs map=new CompanyProfitLogs();
        map.setProfitId(profitId);
        List<CompanyProfitLogs> logs= logsService.selectCompanyProfitLogsList(map);
        return R.ok().put("profit",profit).put("logs",logs);
    }

    /**
     * 新增提现
     */
    @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:audit1')")
    @PostMapping("/audit1")
    @Transactional
    public R audit1(@RequestBody CompanyProfitAuditParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        CompanyProfit profit=companyProfitService.selectCompanyProfitById(param.getProfitId());
        if(profit.getProfitStatus()!=1){
            return R.error("非法操作");
        }
        Company company=companyService.selectCompanyByIdForUpdate(profit.getCompanyId());
        if(param.getStatus()==1){
            profit.setRemark(param.getRemark());
            profit.setProfitStatus(2);
            profit.setUpdateTime(new Date());
            companyProfitService.updateCompanyProfit(profit);
            CompanyProfitLogs logs=new CompanyProfitLogs();
            logs.setProfitId(profit.getProfitId());
            logs.setReason(param.getRemark());
            logs.setCreateTime(new Date());
            logs.setTitle(loginUser.getUser().getNickName()+"审核通过");
            logsService.insertCompanyProfitLogs(logs);
        }
        else if(param.getStatus()==0){
            //驳回退款
            profit.setRemark(param.getRemark());
            profit.setProfitStatus(0);
            profit.setUpdateTime(new Date());
            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(8);
            logs.setBalance(company.getMoney());
            logs.setRemark("驳回退款:"+profit.getMoney());
            logs.setCreateTime(new Date());
            moneyLogsService.insertCompanyMoneyLogs(logs);

            CompanyProfitLogs profitLogs=new CompanyProfitLogs();
            profitLogs.setProfitId(profit.getProfitId());
            profitLogs.setReason(param.getRemark());
            profitLogs.setCreateTime(new Date());
            profitLogs.setTitle(loginUser.getUser().getNickName()+"驳回退款");
            logsService.insertCompanyProfitLogs(profitLogs);
        }
        return R.ok("操作成功");
    }

    @PreAuthorize("@ss.hasPermi('company:companyProfit:audit2')")
    @PostMapping("/audit2")
    @Transactional
    public R audit2(@RequestBody CompanyProfitAuditParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        CompanyProfit profit=companyProfitService.selectCompanyProfitById(param.getProfitId());
        if(profit.getProfitStatus()!=2){
            return R.error("非法操作");
        }

        if(param.getStatus()==1){
            profit.setRemark(param.getRemark());
            profit.setProfitStatus(4);
            profit.setUpdateTime(new Date());
            companyProfitService.updateCompanyProfit(profit);
            CompanyProfitLogs logs=new CompanyProfitLogs();
            logs.setProfitId(profit.getProfitId());
            logs.setReason(param.getRemark());
            logs.setCreateTime(new Date());
            logs.setTitle(loginUser.getUser().getNickName()+"审核通过");
            logsService.insertCompanyProfitLogs(logs);
        }
        else if(param.getStatus()==0){
            //驳回退款
            profit.setRemark(param.getRemark());
            profit.setProfitStatus(0);
            profit.setUpdateTime(new Date());
            companyProfitService.updateCompanyProfit(profit);
            Company company=companyService.selectCompanyByIdForUpdate(profit.getCompanyId());
            company.setMoney(company.getMoney().add(profit.getMoney()));
            companyService.updateCompany(company);
            CompanyMoneyLogs logs=new CompanyMoneyLogs();
            logs.setCompanyId(company.getCompanyId());
            logs.setMoney(profit.getMoney());
            logs.setLogsType(8);
            logs.setBalance(company.getMoney());
            logs.setRemark("驳回退款:"+profit.getMoney());
            logs.setCreateTime(new Date());
            moneyLogsService.insertCompanyMoneyLogs(logs);

            CompanyProfitLogs profitLogs=new CompanyProfitLogs();
            profitLogs.setProfitId(profit.getProfitId());
            profitLogs.setReason(param.getRemark());
            profitLogs.setCreateTime(new Date());
            profitLogs.setTitle(loginUser.getUser().getNickName()+"驳回退款");
            logsService.insertCompanyProfitLogs(profitLogs);
        }
        return R.ok("操作成功");
    }

    @PreAuthorize("@ss.hasPermi('company:companyProfit:audit3')")
    @PostMapping("/audit3")
    @Transactional
    public R audit3(@RequestBody CompanyProfitAuditParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        CompanyProfit profit=companyProfitService.selectCompanyProfitById(param.getProfitId());
        if(profit.getProfitStatus()!=3){
            return R.error("非法操作");
        }
        Company company=companyService.selectCompanyByIdForUpdate(profit.getCompanyId());
        if(param.getStatus()==1){
            profit.setRemark(param.getRemark());
            profit.setProfitStatus(4);
            profit.setUpdateTime(new Date());
            profit.setImgUrl(param.getImgUrl());
            companyProfitService.updateCompanyProfit(profit);
            CompanyProfitLogs logs=new CompanyProfitLogs();
            logs.setProfitId(profit.getProfitId());
            logs.setReason(param.getRemark());
            logs.setCreateTime(new Date());
            logs.setTitle(loginUser.getUser().getNickName()+"审核通过");
            logsService.insertCompanyProfitLogs(logs);
        }
        else if(param.getStatus()==0){
            //驳回退款
            profit.setRemark(param.getRemark());
            profit.setProfitStatus(0);
            profit.setUpdateTime(new Date());
            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(8);
            logs.setBalance(company.getMoney());
            logs.setRemark("驳回退款:"+profit.getMoney());
            logs.setCreateTime(new Date());
            moneyLogsService.insertCompanyMoneyLogs(logs);

            CompanyProfitLogs profitLogs=new CompanyProfitLogs();
            profitLogs.setProfitId(profit.getProfitId());
            profitLogs.setReason(param.getRemark());
            profitLogs.setCreateTime(new Date());
            profitLogs.setTitle(loginUser.getUser().getNickName()+"驳回退款");
            logsService.insertCompanyProfitLogs(profitLogs);
        }
        return R.ok("操作成功");
    }


}
