package com.huiju.estateagents.redpack.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.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.common.StringUtils;
import com.huiju.estateagents.redpack.entity.TaOrgAccount;
import com.huiju.estateagents.redpack.entity.TaOrgOrder;
import com.huiju.estateagents.redpack.entity.TaOrgRefundApplication;
import com.huiju.estateagents.redpack.service.ITaOrgAccountService;
import com.huiju.estateagents.redpack.service.ITaOrgOrderService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 系统账户表  前端控制器
 * </p>
 *
 * @author fxf
 * @since 2020-06-03
 */
@Slf4j
@RestController
@RequestMapping("/api")
public class TaOrgAccountController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(TaOrgAccountController.class);

    @Autowired
    public ITaOrgAccountService iTaOrgAccountService;

    @Autowired
    public ITaOrgOrderService iTaOrgOrderService;

    /**
     * 分页查询列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/api/taOrgAccount", method = RequestMethod.GET)
    public ResponseBean taOrgAccountList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "miniAppName", required = false) String miniAppName) {
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
            IPage<TaOrgAccount> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaOrgAccount> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(!StringUtils.isEmpty(miniAppName), "miniapp_name", miniAppName);
            queryWrapper.orderByDesc("create_date");

            IPage<TaOrgAccount> result = iTaOrgAccountService.page(pg, queryWrapper);
            responseBean.addSuccess(result);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taOrgAccountList -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 保存对象
     *
     * @param taOrgAccount 实体对象
     * @return
     */
    @RequestMapping(value = "/api/taOrgAccount", method = RequestMethod.POST)
    public ResponseBean taOrgAccountAdd(@RequestBody TaOrgAccount taOrgAccount) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTaOrgAccountService.save(taOrgAccount)) {
                responseBean.addSuccess(taOrgAccount);
            } else {
                responseBean.addError("fail");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taOrgAccountAdd -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     */
    @ResponseBody
    @RequestMapping(value = "/api/taOrgAccount/{id}", method = RequestMethod.DELETE)
    public ResponseBean taOrgAccountDelete(@PathVariable Integer id) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTaOrgAccountService.removeById(id)) {
                responseBean.addSuccess("success");
            } else {
                responseBean.addError("fail");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taOrgAccountDelete -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 修改对象
     *
     * @param id           实体ID
     * @param taOrgAccount 实体对象
     * @return
     */
    @RequestMapping(value = "/api/taOrgAccount/{id}", method = RequestMethod.PUT)
    public ResponseBean taOrgAccountUpdate(@PathVariable Integer id,
                                           @RequestBody TaOrgAccount taOrgAccount) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTaOrgAccountService.updateById(taOrgAccount)) {
                responseBean.addSuccess(taOrgAccount);
            } else {
                responseBean.addError("fail");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taOrgAccountUpdate -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id查询对象
     *
     * @param id 实体ID
     */
    @RequestMapping(value = "/channel/taOrgAccount/{id}", method = RequestMethod.GET)
    public ResponseBean taOrgAccountGet(@PathVariable Integer id) {
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTaOrgAccountService.getOrgAccountById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taOrgAccountDelete -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 系统账户充值
     *
     * @param id 实体ID
     */
    @RequestMapping(value = "/channel/taOrgAccount/recharge/{id}", method = RequestMethod.PUT)
    public ResponseBean taOrgAccountRecharge(@PathVariable String id, @RequestBody TaOrgAccount taOrgAccount,
                                             HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        try {
            Integer orgId = getOrgId(request);
            Integer userId = getUserId(request);
            iTaOrgAccountService.rechargeAccount(id, taOrgAccount, 26, 2149);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taOrgAccountRecharge -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 系统账户退款
     *
     * @param id
     * @param taOrgAccount
     * @param request
     * @return
     */
    @RequestMapping(value = "/channel/taOrgAccount/refund/{id}", method = RequestMethod.PUT)
    public ResponseBean taOrgAccountRefund(@PathVariable String id, @RequestBody TaOrgAccount taOrgAccount,
                                           HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        try {
            Integer orgId = getOrgId(request);
            Integer userId = getUserId(request);
            responseBean = iTaOrgAccountService.refundAccount(id, taOrgAccount, 26, 2149);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taOrgAccountRefund -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 订单退款
     *
     * @param id
     * @param refundApplication
     * @param request
     * @return
     */
    @RequestMapping(value = "/channel/taOrgOrder/refund/{id}", method = RequestMethod.PUT)
    public ResponseBean taOrgOrderRefund(@PathVariable String id, @RequestBody TaOrgRefundApplication refundApplication,
                                         HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        try {
            Integer userId = getUserId(request);
            responseBean = iTaOrgOrderService.refundOrder(id, refundApplication, 2149);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taOrgAccountRefund -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 订单驳回
     *
     * @param id
     * @param refundApplication
     * @param request
     * @return
     */
    @RequestMapping(value = "/channel/taOrgOrder/reject/{id}")
    public ResponseBean taOrgOrderReject(@PathVariable String id, @RequestBody TaOrgRefundApplication refundApplication,
                                         HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        try {
            Integer userId = getUserId(request);
            responseBean = iTaOrgOrderService.rejectOrder(id, refundApplication, 2149);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taOrgAccountRefund -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }


    /**
     * 获取账户资金信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/admin/orgAccount/get", method = RequestMethod.GET)
    public ResponseBean getOrgAccountInfoByOrgId(HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean = iTaOrgAccountService.getOrgAccountInfoByOrgId(getOrgId(request));
        } catch (Exception e) {
            logger.error("getOrgAccountInfo -=- {}", e);
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 微信统一下单接口
     *
     * @param taOrgOrder
     * @param request
     * @return
     */
    @RequestMapping(value = "/admin/unifiedOrder",method = RequestMethod.POST)
    public ResponseBean wxUnifiedOrder(@RequestBody TaOrgOrder taOrgOrder,
                                       HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            taOrgOrder.setOrgId(getOrgId(request));
            taOrgOrder.setUserId(getUserId(request));
            responseBean = iTaOrgOrderService.payUnifiedOrder(taOrgOrder,request.getRemoteAddr());
        } catch (Exception e) {
            log.error("微信统一下单失败： {}" , e.getMessage());
            return ResponseBean.error(e.getMessage(), ResponseBean.ERROR_MISSING_PARAMS);
        }
        return responseBean;
    }
}
