package com.fs.his.controller;

import java.util.Date;
import java.util.List;

import com.fs.common.core.domain.R;
import com.fs.common.core.domain.model.LoginUser;
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.CompanyRechargeVO;
import com.fs.framework.web.service.TokenService;
import lombok.Synchronized;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.common.core.page.TableDataInfo;

/**
 * 充值管理Controller
 *
 * @author fs
 * @date 2023-07-13
 */
@RestController
@RequestMapping("/his/companyRecharge")
public class FsCompanyRechargeController extends BaseController
{
    @Autowired
    private ICompanyRechargeService companyRechargeService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private ICompanyMoneyLogsService moneyLogsService;

    /**
     * 查询充值管理列表
     */
    @PreAuthorize("@ss.hasPermi('his:companyRecharge:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyRechargeVO companyRecharge)
    {
        startPage();
        List<CompanyRechargeVO> list = companyRechargeService.selectCompanyRechargeListVO(companyRecharge);
        return getDataTable(list);
    }

    /**
     * 导出充值管理列表
     */
    @PreAuthorize("@ss.hasPermi('his:companyRecharge:export')")
    @Log(title = "充值管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyRechargeVO companyRecharge)
    {
        companyRecharge.setIsAudit(null);
        List<CompanyRechargeVO> list = companyRechargeService.selectCompanyRechargeListVO(companyRecharge);
        ExcelUtil<CompanyRechargeVO> util = new ExcelUtil<CompanyRechargeVO>(CompanyRechargeVO.class);
        return util.exportExcel(list, "充值管理数据");
    }

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

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

//    /**
//     * 修改充值管理
//     */
//    @PreAuthorize("@ss.hasPermi('his:companyRecharge:edit')")
//    @Log(title = "充值管理", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody CompanyRecharge companyRecharge)
//    {
//        return toAjax(companyRechargeService.updateCompanyRecharge(companyRecharge));
//    }

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



    @PreAuthorize("@ss.hasPermi('his:companyRecharge:edit')")
    @PutMapping
    @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){
            if(1==companyRecharge.getBusinessType()){// 红包充值
                // 更新红包充值余额redis字段
                companyService.redPacketTopUpCompany(companyRecharge.getCompanyId(),companyRecharge.getMoney(),"1");
            }else {
                Company company=companyService.selectCompanyByIdForUpdate(companyRecharge.getCompanyId());
                company.setMoney(company.getMoney().add(companyRecharge.getMoney()));
                companyRecharge.setBalance(company.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("操作成功");
    }

}
