package com.ruoyi.frequency.userwithdrawalway.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.controller.req.AddUserWithdrawalWayReq;
import com.ruoyi.controller.req.EditUserWithdrawalWayReq;
import com.ruoyi.controller.resp.UserWithdrawalWayResp;
import com.ruoyi.frequency.userwithdrawalway.entity.UserWithdrawalWay;
import com.ruoyi.frequency.userwithdrawalway.mapper.UserWithdrawalWayMapper;
import com.ruoyi.frequency.userwithdrawalway.service.UserWithdrawalWayService;
import com.ruoyi.response.ResponseData;
import com.ruoyi.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 用户提现方式Service业务层处理
 *
 * @author liwenlong
 * @date 2023-07-20
 */
@Service
public class UserWithdrawalWayServiceImpl extends ServiceImpl<UserWithdrawalWayMapper, UserWithdrawalWay>  implements UserWithdrawalWayService
{
    @Autowired
    private UserWithdrawalWayMapper userWithdrawalWayMapper;

    /**
     * 查询用户提现方式
     *
     * @param id 用户提现方式主键
     * @return 用户提现方式
     */
    @Override
    public UserWithdrawalWay selectUserWithdrawalWayById(Long id)
    {
        return userWithdrawalWayMapper.selectUserWithdrawalWayById(id);
    }

    /**
     * 查询用户提现方式列表
     *
     * @param userWithdrawalWay 用户提现方式
     * @return 用户提现方式
     */
    @Override
    public List<UserWithdrawalWay> selectUserWithdrawalWayList(UserWithdrawalWay userWithdrawalWay)
    {
        return userWithdrawalWayMapper.selectUserWithdrawalWayList(userWithdrawalWay);
    }

    /**
     * 新增用户提现方式
     *
     * @param userWithdrawalWay 用户提现方式
     * @return 结果
     */
    @Override
    public int insertUserWithdrawalWay(UserWithdrawalWay userWithdrawalWay)
    {
        userWithdrawalWay.setCreateTime(DateUtils.getNowDate());
        return userWithdrawalWayMapper.insertUserWithdrawalWay(userWithdrawalWay);
    }

    /**
     * 修改用户提现方式
     *
     * @param userWithdrawalWay 用户提现方式
     * @return 结果
     */
    @Override
    public int updateUserWithdrawalWay(UserWithdrawalWay userWithdrawalWay)
    {
        userWithdrawalWay.setUpdateTime(DateUtils.getNowDate());
        return userWithdrawalWayMapper.updateUserWithdrawalWay(userWithdrawalWay);
    }

    /**
     * 批量删除用户提现方式
     *
     * @param ids 需要删除的用户提现方式主键
     * @return 结果
     */
    @Override
    public int deleteUserWithdrawalWayByIds(Long[] ids)
    {
        return userWithdrawalWayMapper.deleteUserWithdrawalWayByIds(ids);
    }

    /**
     * 删除用户提现方式信息
     *
     * @param id 用户提现方式主键
     * @return 结果
     */
    @Override
    public int deleteUserWithdrawalWayById(Long id)
    {
        return userWithdrawalWayMapper.deleteUserWithdrawalWayById(id);
    }

    @Override
    public ResponseData selList(UserVo userVo) {
        List<UserWithdrawalWayResp> userWithdrawalWayRespList = baseMapper.selList(userVo.getUserId(), userVo.getUserType());
        return ResponseData.success(userWithdrawalWayRespList);
    }

    @Override
    public ResponseData defaultWay(UserVo userVo) {

        UserWithdrawalWayResp defaultWay = baseMapper.defaultWay(userVo.getUserId(), userVo.getUserType());

        return ResponseData.success(defaultWay);
    }

    @Override
    public ResponseData getDetail(Long id) {

        UserWithdrawalWayResp userWithdrawalWayVO = baseMapper.getDetail(id);
        return ResponseData.success(userWithdrawalWayVO);

    }

    @Override
    public ResponseData addOne(UserVo userVo, AddUserWithdrawalWayReq req) {

        //默认提现方式改为非默认
        if (ObjectUtil.equal(PublicCommon.默认,req.getIsDefault())) {
            this.lambdaUpdate().set(UserWithdrawalWay::getIsDefault,PublicCommon.非默认)
                    .eq(UserWithdrawalWay::getDelFlag,PublicCommon.启用)
                    .eq(UserWithdrawalWay::getIsDefault,PublicCommon.默认)
                    .eq(UserWithdrawalWay::getUserType,userVo.getUserType())
                    .eq(UserWithdrawalWay::getUserId,userVo.getUserId()).update();
        }

        UserWithdrawalWay userWithdrawalWay = new UserWithdrawalWay();
        BeanUtil.copyProperties(req,userWithdrawalWay);

        userWithdrawalWay.setUserId(userVo.getUserId())
                .setUserType(userVo.getUserType());

        userWithdrawalWay.setCreateTime(new Date())
                .setUpdateTime(new Date());

        baseMapper.insert(userWithdrawalWay);

        return ResponseData.success();


    }

    @Override
    public ResponseData editOne(EditUserWithdrawalWayReq req, UserVo userVo) {

        UserWithdrawalWay userWithdrawalWay = baseMapper.selectById(req.getId());

        //默认提现方式改为非默认
        if (ObjectUtil.equal(PublicCommon.默认,req.getIsDefault())) {
            this.lambdaUpdate().set(UserWithdrawalWay::getIsDefault,PublicCommon.非默认)
                    .eq(UserWithdrawalWay::getDelFlag,PublicCommon.启用)
                    .eq(UserWithdrawalWay::getIsDefault,PublicCommon.默认)
                    .eq(UserWithdrawalWay::getUserType,userVo.getUserType())
                    .eq(UserWithdrawalWay::getUserId,userVo.getUserId()).update();
        }

        userWithdrawalWay.setCashAccount(req.getCashAccount())
                .setContactWay(req.getContactWay())
                .setLinkman(req.getLinkman())
                .setDelFlag(PublicCommon.启用)
                .setPfWayId(req.getPfWayId())
                .setIsDefault(req.getIsDefault())
                .setAccountOpeningSubBranch(req.getAccountOpeningSubBranch());

        baseMapper.updateById(userWithdrawalWay);

        return ResponseData.success();

    }

    @Override
    public ResponseData deleteOne(Long id) {
        this.lambdaUpdate().set(UserWithdrawalWay::getDelFlag,PublicCommon.删除).eq(UserWithdrawalWay::getId,id).update();
        return ResponseData.success();
    }
}
