package com.sqx.modules.shop.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.dao.UserDao;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserMoneyDetailsService;
import com.sqx.modules.app.service.UserMoneyService;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.datacentre.entity.SysUserShop;
import com.sqx.modules.exportExecl.dto.AccountEntry;
import com.sqx.modules.goods.dao.GoodsShopDao;
import com.sqx.modules.goods.entity.GoodsShop;
import com.sqx.modules.pay.controller.app.AliPayController;
import com.sqx.modules.pay.controller.query.CashOutQueryDTO;
import com.sqx.modules.pay.service.CashOutService;
import com.sqx.modules.pay.service.WxService;
import com.sqx.modules.sys.controller.AbstractController;
import com.sqx.modules.sys.dao.SysUserDao;
import com.sqx.modules.sys.entity.SysUserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@Api(tags={"商户端-商户钱包"})
@RestController
@RequestMapping("/shop/shopmoney")
public class ShopMoneyController extends AbstractController {

    @Autowired
    private UserMoneyService userMoneyService;
    @Autowired
    private UserMoneyDetailsService userMoneyDetailsService;
    @Autowired
    private UserDao userDao;
    @Autowired
    private CashOutService cashOutService;
    @Autowired
    private UserService userService;
    @Autowired
    private WxService wxService;
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private AliPayController aliPayController;
    @Autowired
    private GoodsShopDao goodsShopDao;

    @GetMapping("/selectShopMoney")
    @ApiOperation("商户钱包余额")
    public Result selectShopMoney() {
        SysUserEntity user = getUser();
        Long adminUserId = user.getUserId();
        UserEntity userEntity = userDao.selectOne(new QueryWrapper<UserEntity>().eq("admin_user_id", adminUserId));
        return Result.success().put("data", userMoneyService.selectUserMoneyByUserId(userEntity.getUserId()));
    }

    @GetMapping("/selectShopMoneyByShopId")
    @ApiOperation("商户钱包余额")
    public Result selectShopMoney(Long shopId) {
        GoodsShop goodsShop = goodsShopDao.selectById(shopId);
        return Result.success().put("data", userMoneyService.selectUserMoneyByUserId(goodsShop.getUserId()));
    }

    @ApiOperation("商户缴纳保证金")
    @PostMapping(value = "shopCashDeposit")
    public Result shopCashDeposit(String openId, Double money,Integer type) throws Exception {
        SysUserEntity user = getUser();
        Long adminUserId = user.getUserId();
        SysUserShop sysUserShop = sysUserDao.selectUserShop(adminUserId);
//        UserEntity userEntity = userDao.selectOne(new QueryWrapper<UserEntity>().eq("admin_user_id", adminUserId));
        //type作为分类 1 2 3为小程序  4为支付宝app 5为支付宝h5
        if(type==4 || type==5){
            return aliPayController.payShopCashDeposit(sysUserShop.getShopId(), BigDecimal.valueOf(money),type);
        }
        return wxService.shopCashDeposit(sysUserShop.getShopId(), money, openId,type);
    }

    @GetMapping(value = "/shopCashMoney")
    @ApiOperation("商户发起提现")
    public Result shopCashMoney(Double money) {
        SysUserEntity user = getUser();
        Long adminUserId = user.getUserId();
        UserEntity userEntity = userDao.selectOne(new QueryWrapper<UserEntity>().eq("admin_user_id", adminUserId));
        SysUserShop sysUserShop = sysUserDao.selectUserShop(adminUserId);
        // classify为5标识银行卡提现
        return cashOutService.cashMoney(userEntity.getUserId(), sysUserShop.getShopId(),money,5, 2);
    }

    @GetMapping(value = "shopBindingQrCode")
    @ApiOperation("商户绑定提现二维码")
    public Result shopBindingQrCode(String cashQrCode){
        SysUserEntity user = getUser();
        Long adminUserId = user.getUserId();
        UserEntity userEntity = userDao.selectOne(new QueryWrapper<UserEntity>().eq("admin_user_id", adminUserId));
        UserEntity userEntity1 = new UserEntity();
        userEntity1.setUserId(userEntity.getUserId());
        userEntity1.setCashQrCode(cashQrCode);
        userDao.updateById(userEntity1);
        return Result.success();
    }

    @RequestMapping(value = "/selectCashOutList", method = RequestMethod.GET)
    @ApiOperation("查询提现记录列表")
    @ResponseBody
    public Result selectHelpProfit(Integer page, Integer limit, Long shopId){
        CashOutQueryDTO queryDTO = new CashOutQueryDTO();
        if (page != null) {
            queryDTO.setPage(page);
        }

        if (limit != null) {
            queryDTO.setLimit(limit);
        }

        if(shopId != null){
            GoodsShop goodsShop = goodsShopDao.selectById(shopId);
            queryDTO.setUserId(goodsShop.getUserId());
        }else{
            SysUserEntity user = getUser();
            Long adminUserId = user.getUserId();
            UserEntity userEntity = userDao.selectOne(new QueryWrapper<UserEntity>().eq("admin_user_id", adminUserId));
            queryDTO.setUserId(userEntity.getUserId());
        }

        PageUtils pageUtils = cashOutService.selectCashOutList(queryDTO);
        return Result.success().put("data",pageUtils);
    }

    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    @ApiOperation("修改当前登录商户的提现信息")
    public Result updateUser(@RequestBody UserEntity userEntity) {
        SysUserEntity user = getUser();
        Long adminUserId = user.getUserId();
        UserEntity userEntity1 = userDao.selectOne(new QueryWrapper<UserEntity>().eq("admin_user_id", adminUserId));
        userEntity.setUserId(userEntity1.getUserId());
        userService.updateById(userEntity);
        return Result.success();
    }

    @RequestMapping(value = "/updateUserByShopId", method = RequestMethod.POST)
    @ApiOperation("修改指定商户的提现信息")
    public Result updateUserByShopId(@RequestBody UserEntity userEntity) {
        GoodsShop goodsShop = goodsShopDao.selectById(userEntity.getShopId());
        userEntity.setUserId(goodsShop.getUserId());
        userService.updateById(userEntity);
        return Result.success();
    }

    @GetMapping(value = "/selectShopMoneydetails")
    @ApiOperation("商户钱包明细")
    public Result selectShopMoneydetails(Integer page, Integer limit, Integer classify, Integer type){
        SysUserEntity user = getUser();
        Long adminUserId = user.getUserId();
        SysUserShop sysUserShop = sysUserDao.selectUserShop(adminUserId);
        return userMoneyDetailsService.selectShopMoneyDetails(page, limit, sysUserShop.getShopId(), classify, type);
    }

    @GetMapping(value = "/selectShopMoneydetailsByShopId")
    @ApiOperation("商户钱包明细")
    public Result selectShopMoneydetailsByShopId(Integer page, Integer limit, Integer classify, Integer type,Long shopId){
        return userMoneyDetailsService.selectShopMoneyDetails(page, limit, shopId, classify, type);
    }

    @ApiOperation("商户保证金明细")
    @GetMapping(value = "selectShopCashDeposit")
    public Result selectShopCashDeposit(Integer page, Integer limit, Integer classify){
        SysUserEntity user = getUser();
        Long adminUserId = user.getUserId();
        SysUserShop sysUserShop = sysUserDao.selectUserShop(adminUserId);
        return userMoneyDetailsService.selectShopCashDeposit(sysUserShop.getShopId(), page, limit, classify);
    }

    @GetMapping(value = "/accountEntryManagement")
    @ApiOperation("入账管理")
    public Result accountEntryManagement(Integer page, Integer limit,String shopName,String shopPhone,String userPhone,String couponName,String orderNumber,String startTime,String endTime,String flag){
        PageUtils pageUtils = userMoneyDetailsService.accountEntryManagementDetails(page, limit, shopName, shopPhone, userPhone, couponName, orderNumber, startTime, endTime,flag);
        return Result.success().put("data", pageUtils);
    }

    @GetMapping(value = "/accountEntryAbnormal")
    @ApiOperation("异常入账管理")
    public Result accountEntryAbnormal(AccountEntry accountEntry){
        return userMoneyDetailsService.accountEntryAbnormal(accountEntry);

    }

}
