package com.fs.his.service.impl;

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

import com.fs.common.exception.CustomException;
import com.fs.common.utils.DateUtils;
import com.fs.his.domain.FsStore;
import com.fs.his.domain.FsStoreBill;
import com.fs.his.mapper.FsStoreBillMapper;
import com.fs.his.mapper.FsStoreMapper;
import com.fs.his.vo.FsStoreExtractVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.his.mapper.FsStoreExtractMapper;
import com.fs.his.domain.FsStoreExtract;
import com.fs.his.service.IFsStoreExtractService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 店铺提现管理Service业务层处理
 *
 * @author fs
 * @date 2023-06-25
 */
@Service
public class FsStoreExtractServiceImpl implements IFsStoreExtractService
{
    @Autowired
    private FsStoreExtractMapper fsStoreExtractMapper;
    @Autowired
    private FsStoreMapper fsStoreMapper;
    @Autowired
    private FsStoreBillMapper fsStoreBillMapper;
    /**
     * 查询店铺提现管理
     *
     * @param extractId 店铺提现管理主键
     * @return 店铺提现管理
     */
    @Override
    public FsStoreExtract selectFsStoreExtractByExtractId(String extractId)
    {
        return fsStoreExtractMapper.selectFsStoreExtractByExtractId(extractId);
    }

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

    /**
     * 新增店铺提现管理
     *
     * @param fsStoreExtract 店铺提现管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFsStoreExtract(FsStoreExtract fsStoreExtract)
    {
        fsStoreExtract.setCreateTime(DateUtils.getNowDate());
        FsStore 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);
        FsStoreBill fsStoreBill = new FsStoreBill();
        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(FsStoreExtract 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(FsStoreExtract fsStoreExtract) {
        return fsStoreExtractMapper.selectFsStoreExtractListVO(fsStoreExtract);
    }
}
