package com.fs.store.service.impl;

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

import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.store.domain.FsUser;
import com.fs.store.enums.BillDetailEnum;
import com.fs.store.mapper.FsUserMapper;
import com.fs.store.param.FsUseDoExtractParam;
import com.fs.store.param.FsUseExtractQueryParam;
import com.fs.store.service.IFsUserBillService;
import com.fs.store.vo.FsUserExtractVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.store.mapper.FsUserExtractMapper;
import com.fs.store.domain.FsUserExtract;
import com.fs.store.service.IFsUserExtractService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户提现Service业务层处理
 * 
 * @author fs
 * @date 2022-03-15
 */
@Service
public class FsUserExtractServiceImpl implements IFsUserExtractService 
{
    @Autowired
    private FsUserExtractMapper fsUserExtractMapper;
    @Autowired
    private FsUserMapper userMapper;
    @Autowired
    private IFsUserBillService billService;
    /**
     * 查询用户提现
     * 
     * @param id 用户提现ID
     * @return 用户提现
     */
    @Override
    public FsUserExtract selectFsUserExtractById(Long id)
    {
        return fsUserExtractMapper.selectFsUserExtractById(id);
    }

    /**
     * 查询用户提现列表
     * 
     * @param fsUserExtract 用户提现
     * @return 用户提现
     */
    @Override
    public List<FsUserExtract> selectFsUserExtractList(FsUserExtract fsUserExtract)
    {
        return fsUserExtractMapper.selectFsUserExtractList(fsUserExtract);
    }

    /**
     * 新增用户提现
     * 
     * @param fsUserExtract 用户提现
     * @return 结果
     */
    @Override
    public int insertFsUserExtract(FsUserExtract fsUserExtract)
    {
        fsUserExtract.setCreateTime(DateUtils.getNowDate());
        return fsUserExtractMapper.insertFsUserExtract(fsUserExtract);
    }

    /**
     * 修改用户提现
     * 
     * @param fsUserExtract 用户提现
     * @return 结果
     */
    @Override
    public int updateFsUserExtract(FsUserExtract fsUserExtract)
    {
        fsUserExtract.setUpdateTime(DateUtils.getNowDate());
        return fsUserExtractMapper.updateFsUserExtract(fsUserExtract);
    }

    /**
     * 批量删除用户提现
     * 
     * @param ids 需要删除的用户提现ID
     * @return 结果
     */
    @Override
    public int deleteFsUserExtractByIds(Long[] ids)
    {
        return fsUserExtractMapper.deleteFsUserExtractByIds(ids);
    }

    /**
     * 删除用户提现信息
     * 
     * @param id 用户提现ID
     * @return 结果
     */
    @Override
    public int deleteFsUserExtractById(Long id)
    {
        return fsUserExtractMapper.deleteFsUserExtractById(id);
    }

    @Override
    @Transactional
    public R doExtract(FsUseDoExtractParam param) {
        if(param.getExtractPrice().compareTo(new BigDecimal(100))==-1){
            return R.error("提现金额不得低于100元");
        }
        BigDecimal r = param.getExtractPrice().remainder(new BigDecimal(100));
        if(r.compareTo(new BigDecimal(0))!=0){
            return R.error("提现金额必须为100的整数陪");
        }
        //查询用户是否提现金额是否大于用户余额
        FsUser user=userMapper.selectFsUserById(param.getUserId());
        if(user.getStatus()==0){
            return R.error("该帐号已禁用,请联系管理员");
        }
        if(user.getNowMoney().compareTo(param.getExtractPrice())==-1){
            return R.error("余额不足，不能提现");
        }
        user.setNowMoney(user.getNowMoney().subtract(param.getExtractPrice()));
        FsUserExtract extract=new FsUserExtract();
        extract.setUserId(user.getUserId());
        extract.setExtractType("wx");
        extract.setWechat(user.getMaOpenId());
        extract.setExtractPrice(param.getExtractPrice());
        extract.setBalance(user.getNowMoney());
        extract.setStatus(0);
        extract.setCreateTime(new Date());
        fsUserExtractMapper.insertFsUserExtract(extract);
        userMapper.updateFsUser(user);
        //插入流水
        billService.addBill(user.getUserId(), BillDetailEnum.CATEGORY_1.getValue(),0,BillDetailEnum.TYPE_6.getDesc(),param.getExtractPrice().doubleValue(),user.getNowMoney().doubleValue(),"提现申请",extract.getId().toString(),0l);

        return R.ok("提交成功,等待后台审核");
    }

    @Override
    public List<FsUserExtractVO> selectFsUserExtractListVOQuery(FsUseExtractQueryParam param) {
        return fsUserExtractMapper.selectFsUserExtractListVOQuery(param);
    }
}
