package com.xhpc.activity.controller;


import com.xhpc.activity.domain.XhpcClearingBankDomain;
import com.xhpc.activity.service.XhpcClearingBankService;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.exception.CustomException;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.common.log.annotation.Log;
import com.xhpc.common.log.enums.BusinessType;
import com.xhpc.common.util.LogUserUtils;
import com.xhpc.system.api.model.LoginUser;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/clearing/bank")
public class XhpcClearingBankController extends BaseController {

    @Resource
    XhpcClearingBankService bankService;

    @Resource
    LogUserUtils logUserUtils;


    @GetMapping("/getPage")
    public TableDataInfo getPage(HttpServletRequest request,
                                 String status) {

        LoginUser loginUser = logUserUtils.getLogUser(request);
        if (loginUser.getSysUser().getOperatorId() == null){
            throw new CustomException("请登陆运营商账户再试");
        }

        startPage();
        Map<String, Object> params = new HashMap<>();
        params.put("tenantId", loginUser.getTenantId());
        params.put("status", status);
        params.put("operatorId", loginUser.getSysUser().getOperatorId());
        return getDataTable(bankService.getPage(params));
    }


    @GetMapping("/detail")
    public R getDetail(@RequestParam("bankId") Long bankId) {

        return R.ok(bankService.getDomainByPk(bankId));
    }

    @Log(title = "清分管理-发送绑定银行卡验证短信", businessType = BusinessType.INSERT)
    @PostMapping("/sendBindSms")
    public R sendBindBankSms(String phone){

        return R.ok(bankService.sendBindBankSms(phone));
    }


    @Log(title = "清分管理-发送提现到银行卡验证短信", businessType = BusinessType.INSERT)
    @PostMapping("/sendCashSms")
    public R sendCashSms(String phone){
        
        return R.ok(bankService.sendCashSms(phone));
    }


    @Log(title = "清分管理-增加清分转账银行信息", businessType = BusinessType.INSERT)
    @PostMapping("/")
    public R insertDomain(HttpServletRequest request, @RequestBody XhpcClearingBankDomain domain) {

        LoginUser loginUser = logUserUtils.getLogUser(request);
        if(domain.getOperatorId() == null){
            domain.setOperatorId(loginUser.getSysUser().getOperatorId());
        }

        domain.setCreateBy(loginUser.getUserid().toString());
        domain.setUpdateBy(loginUser.getUserid().toString());
        domain.setTenantId(loginUser.getTenantId());
        return R.ok(bankService.insertDomain(domain));
    }


    @Log(title = "清分管理-更新清分转账银行信息", businessType = BusinessType.UPDATE)
    @PutMapping("/detail")
    public R updateDomain(HttpServletRequest request, @RequestBody XhpcClearingBankDomain domain) {

        LoginUser loginUser = logUserUtils.getLogUser(request);
        domain.setUpdateBy(loginUser.getUserid().toString());
        return R.ok(bankService.updateDomain(domain));
    }


    @Log(title = "清分管理-删除现金红包活动", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bankId}")
    public R deleteDomainByPk(@PathVariable("bankId") Long bankId) {

        return R.ok(bankService.deleteByPk(bankId));
    }

}
