package com.fs.his.controller;

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

import com.fs.common.annotation.RepeatSubmit;
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.CompanyDeduct;
import com.fs.company.domain.CompanyMoneyLogs;
import com.fs.company.service.ICompanyDeductService;
import com.fs.company.service.ICompanyMoneyLogsService;
import com.fs.company.service.ICompanyService;
import com.fs.company.vo.CompanyDeductVO;
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/companyDeduct")
public class FsCompanyDeductController extends BaseController
{
    @Autowired
    private ICompanyDeductService companyDeductService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private ICompanyMoneyLogsService moneyLogsService;

    /**
     * 查询扣款管理列表
     */
    @PreAuthorize("@ss.hasPermi('his:companyDeduct:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyDeductVO companyDeduct)
    {
        startPage();
        List<CompanyDeductVO> list = companyDeductService.selectCompanyDeductListVO(companyDeduct);

        return getDataTable(list);
    }

    /**
     * 导出扣款管理列表
     */
    @PreAuthorize("@ss.hasPermi('his:companyDeduct:export')")
    @Log(title = "扣款管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyDeductVO companyDeduct)
    {
        companyDeduct.setIsAudit(null);
        List<CompanyDeductVO> list = companyDeductService.selectCompanyDeductListVO(companyDeduct);
        ExcelUtil<CompanyDeductVO> util = new ExcelUtil<CompanyDeductVO>(CompanyDeductVO.class);
        return util.exportExcel(list, "扣款管理数据");
    }

    /**
     * 获取扣款管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('his:companyDeduct:query')")
    @GetMapping(value = "/{deductId}")
    public AjaxResult getInfo(@PathVariable("deductId") Long deductId)
    {
        return AjaxResult.success(companyDeductService.selectCompanyDeductById(deductId));
    }

    /**
     * 新增扣款管理
     */
    @PreAuthorize("@ss.hasPermi('his:companyDeduct:add')")
    @Log(title = "扣款管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompanyDeduct companyDeduct)
    {
        return toAjax(companyDeductService.insertCompanyDeduct(companyDeduct));
    }

//    /**
//     * 修改扣款管理
//     */
//    @PreAuthorize("@ss.hasPermi('his:companyDeduct:edit')")
//    @Log(title = "扣款管理", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody CompanyDeduct companyDeduct)
//    {
//        return toAjax(companyDeductService.updateCompanyDeduct(companyDeduct));
//    }

    /**
     * 删除扣款管理
     */
    @PreAuthorize("@ss.hasPermi('his:companyDeduct:remove')")
    @Log(title = "扣款管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deductIds}")
    public AjaxResult remove(@PathVariable Long[] deductIds)
    {
        return toAjax(companyDeductService.deleteCompanyDeductByIds(deductIds));
    }


    @PreAuthorize("@ss.hasPermi('his:companyDeduct:edit')")
    @PutMapping
    @Transactional
    @RepeatSubmit
    public R audit(@RequestBody CompanyDeduct param)
    {
        CompanyDeduct deduct=companyDeductService.selectCompanyDeductById(param.getDeductId());
        if(deduct.getIsAudit()!=0){
            return R.error("非法操作");
        }
        deduct.setIsAudit(param.getIsAudit());
        deduct.setRemark(param.getRemark());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if(deduct.getIsAudit()==1){
            if(1==param.getBusinessType()){// 红包充值
                // 更新红包充值余额redis字段
                companyService.redPacketTopUpCompany(deduct.getCompanyId(),deduct.getMoney(),"2");
            }else {
                Company company=companyService.selectCompanyByIdForUpdate(deduct.getCompanyId());
                company.setMoney(company.getMoney().subtract(deduct.getMoney()));
                deduct.setBalance(company.getMoney());
                companyService.updateCompany(company);
                //生成流水
                CompanyMoneyLogs log=new CompanyMoneyLogs();
                log.setCompanyId(deduct.getCompanyId());
                log.setMoney(deduct.getMoney().multiply(new BigDecimal(-1)));
                log.setRemark(deduct.getRemark());
                log.setLogsType(2);
                log.setBalance(company.getMoney());
                log.setCreateTime(new Date());
                moneyLogsService.insertCompanyMoneyLogs(log);
            }
        }
        deduct.setAuditTime(new Date());
        deduct.setAuditUserId(loginUser.getUser().getUserId());
        companyDeductService.updateCompanyDeduct(deduct);
        return R.ok("操作成功");

    }

}
