package com.sqx.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.entity.UserMoney;
import com.sqx.modules.app.entity.UserMoneyArtificer;
import com.sqx.modules.app.entity.UserMoneyDetails;
import com.sqx.modules.app.service.UserMoneyArtificerService;
import com.sqx.modules.app.service.UserMoneyDetailsService;
import com.sqx.modules.app.service.UserMoneyService;
import com.sqx.modules.artificer.entity.Artificer;
import com.sqx.modules.artificer.entity.ArtificerTime;
import com.sqx.modules.artificer.service.ArtificerService;
import com.sqx.modules.common.service.CommonInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/Details")
@Api("钱包明细")
public class UserMoneyDetailsController {

    @Autowired
    private UserMoneyDetailsService userMoneyDetailsService;
    @Autowired
    private UserMoneyArtificerService userMoneyArtificerService;
    @Autowired
    private ArtificerService artificerService;

    @Autowired
    private UserMoneyService userMoneyService;

    @ApiOperation("钱包明细")
    @GetMapping("/queryUserMoneyDetails")
    public Result queryUserMoneyDetails(Integer page, Integer limit, Long userId,Integer classify,Integer type,Long artificerId) {
        return userMoneyDetailsService.queryUserMoneyDetails(page, limit, userId,classify,type,artificerId);
    }

    @GetMapping("/selectUserMoneyArtificer")
    @ApiOperation("查询用户充值师傅钱包")
    public Result selectUserMoneyArtificer(Integer page,Integer limit,Long userId){
        return userMoneyDetailsService.selectUserMoneyArtificer(page, limit, userId,null,null);
    }

    @PostMapping("/updateUserMoneyArtificer")
    @ApiOperation("修改用户充值师傅钱包")
    public Result updateUserMoneyArtificer(Long userId, Long artificerId, BigDecimal money,Integer type){
        UserMoneyArtificer userMoneyArtificer = userMoneyArtificerService.getOne(new QueryWrapper<UserMoneyArtificer>().eq("user_id", userId).eq("artificer_id", artificerId));
        if(userMoneyArtificer==null){
            userMoneyArtificer=new UserMoneyArtificer();
            userMoneyArtificer.setUserId(userId);
            userMoneyArtificer.setArtificerId(artificerId);
            userMoneyArtificer.setMoney(money);
            userMoneyArtificer.setCreateTime(DateUtils.format(new Date()));
            userMoneyArtificerService.save(userMoneyArtificer);
        }else{
            userMoneyArtificerService.updateMoney(type,userId,artificerId,money.doubleValue());
        }
        UserMoneyDetails userMoneyDetails =new UserMoneyDetails();
        userMoneyDetails.setClassify(8);
        userMoneyDetails.setMoney(money);
        userMoneyDetails.setUserId(userId);
        Artificer artificer = artificerService.getById(artificerId);
        if(type==null || type==1){
            userMoneyDetails.setTitle("系统充值");
            userMoneyDetails.setContent("指定技师“"+artificer.getArtificerName()+"”钱包系统充值:"+money);
            userMoneyDetails.setType(1);
        }else{
            userMoneyDetails.setTitle("系统扣除");
            userMoneyDetails.setContent("指定技师“"+artificer.getArtificerName()+"”钱包系统扣除:"+money);
            userMoneyDetails.setType(2);
        }
        userMoneyDetails.setCreateTime(DateUtils.format(new Date()));
        userMoneyDetails.setArtificerId(artificerId);
        //查询用户当前余额
        UserMoney userNowMoney = userMoneyService.selectUserMoneyByUserId(userId);
        //插入余额
        userMoneyDetails.setBalance(userNowMoney.getMoney());
        //插入操作类型为已操作用户余额
        userMoneyDetails.setManipulateType(2);
        userMoneyDetailsService.save(userMoneyDetails);
        return Result.success();
    }


}
