package com.fs.hisStore.service.impl;

import com.fs.common.exception.CustomException;
import com.fs.common.utils.DateUtils;
import com.fs.hisStore.domain.FsStoreScrm;
import com.fs.hisStore.domain.FsStoreBillScrm;
import com.fs.hisStore.domain.FsStoreExtractScrm;
import com.fs.his.vo.FsStoreExtractVO;
import com.fs.hisStore.mapper.FsStoreBillScrmMapper;
import com.fs.hisStore.mapper.FsStoreExtractScrmMapper;
import com.fs.hisStore.mapper.FsStoreScrmMapper;
import com.fs.hisStore.service.IFsStoreExtractScrmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 店铺提现管理Service业务层处理
 *
 * @author fs
 * @date 2023-06-25
 */
@Service
public class FsStoreExtractScrmServiceImpl implements IFsStoreExtractScrmService
{
    @Autowired
    private FsStoreExtractScrmMapper fsStoreExtractMapper;
    @Autowired
    private FsStoreScrmMapper fsStoreMapper;
    @Autowired
    private FsStoreBillScrmMapper fsStoreBillMapper;
    /**
     * 查询店铺提现管理
     *
     * @param extractId 店铺提现管理主键
     * @return 店铺提现管理
     */
    @Override
    public FsStoreExtractScrm selectFsStoreExtractByExtractId(String extractId)
    {
        return fsStoreExtractMapper.selectFsStoreExtractByExtractId(extractId);
    }

    /**
     * 查询店铺提现管理列表
     *
     * @param fsStoreExtract 店铺提现管理
     * @return 店铺提现管理
     */
    @Override
    public List<FsStoreExtractScrm> selectFsStoreExtractList(FsStoreExtractScrm fsStoreExtract)
    {
        return fsStoreExtractMapper.selectFsStoreExtractList(fsStoreExtract);
    }

    /**
     * 新增店铺提现管理
     *
     * @param fsStoreExtract 店铺提现管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFsStoreExtract(FsStoreExtractScrm fsStoreExtract)
    {
        fsStoreExtract.setCreateTime(DateUtils.getNowDate());
        FsStoreScrm fsStore = fsStoreMapper.selectFsStoreByStoreId(fsStoreExtract.getStoreId());

        BigDecimal result = fsStore.getBalance().subtract(fsStoreExtract.getExtractPrice());
        if (result.compareTo(BigDecimal.ZERO)<0){
            throw new CustomException("余额不足");
        }
        fsStoreExtract.setBalance(result);
        fsStore.setBalance(result);
        fsStoreMapper.updateFsStore(fsStore);
        FsStoreBillScrm fsStoreBill = new FsStoreBillScrm();
        fsStoreBill.setStoreId(fsStore.getStoreId());
        fsStoreBill.setBillType(1);
        fsStoreBill.setBalance(result);
        fsStoreBill.setTitle("申请提现");
        fsStoreBill.setMoney(fsStoreExtract.getExtractPrice());
        fsStoreBillMapper.insertFsStoreBill(fsStoreBill);
        return fsStoreExtractMapper.insertFsStoreExtract(fsStoreExtract);
    }

    /**
     * 修改店铺提现管理
     *
     * @param fsStoreExtract 店铺提现管理
     * @return 结果
     */
    @Override
    public int updateFsStoreExtract(FsStoreExtractScrm fsStoreExtract)
    {
        if (fsStoreExtract!=null)throw new CustomException("非法操作");
        if (fsStoreExtract.getStatus()!=null&&fsStoreExtract.getStatus()==1){
            //退款操作
        }

        fsStoreExtract.setUpdateTime(DateUtils.getNowDate());
        fsStoreExtract.setFailTime(DateUtils.getNowDate());
        return fsStoreExtractMapper.updateFsStoreExtract(fsStoreExtract);
    }

    /**
     * 批量删除店铺提现管理
     *
     * @param extractIds 需要删除的店铺提现管理主键
     * @return 结果
     */
    @Override
    public int deleteFsStoreExtractByExtractIds(String[] extractIds)
    {
        return fsStoreExtractMapper.deleteFsStoreExtractByExtractIds(extractIds);
    }

    /**
     * 删除店铺提现管理信息
     *
     * @param extractId 店铺提现管理主键
     * @return 结果
     */
    @Override
    public int deleteFsStoreExtractByExtractId(String extractId)
    {
        return fsStoreExtractMapper.deleteFsStoreExtractByExtractId(extractId);
    }

    @Override
    public List<FsStoreExtractVO> selectFsStoreExtractListVO(FsStoreExtractScrm fsStoreExtract) {
        return fsStoreExtractMapper.selectFsStoreExtractListVO(fsStoreExtract);
    }
}
