package cn.pluss.platform.controller.cash;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.cash.CashService;
import cn.pluss.platform.controller.BaseController;
import cn.pluss.platform.entity.Cash;
import cn.pluss.platform.entity.UserInfo;
import cn.pluss.platform.merchantOrder.MerchantOrderService;
import cn.pluss.platform.userInfo.UserInfoService;
import cn.pluss.platform.util.DateUtils;
import cn.pluss.platform.util.StringUtil;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

/**
 * @author DJH
 */
@Controller
@RequestMapping("cash")
public class CashController extends BaseController<CashService, Cash> {

    @Setter(onMethod_ = {@Autowired})
    private CashService cashService;

    @Setter(onMethod_ = {@Autowired})
    private HttpServletRequest request;

    @Setter(onMethod_ = {@Autowired})
    private UserInfoService userInfoService;

    @Setter(onMethod_ = {@Autowired})
    private MerchantOrderService merchantOrderService;

    /**
     * 引导页
     */
    @GetMapping(value = "/index")
    public String index(Cash condition) {
        String type = request.getParameter("type");
        request.setAttribute("type", type);
        Result<Object> otherData = getOtherData(condition);
        request.setAttribute("otherData",otherData.getData());
        return "cash/cashList";
    }

    @RequestMapping(value = "otherData")
    @ResponseBody
    public Result<Object> getOtherData(Cash condition){
        String nowDate = DateUtils.currentStringDate();
        String startTime = nowDate + " 00:00:00";
        String endTime = nowDate + " 23:59:59";
        Map<String,Object> params = new HashMap<>();
        params.put("startTime", StringUtil.isNotEmpty(condition.getBeginTime()) ? condition.getBeginTime() : startTime);
        params.put("endTime", StringUtil.isNotEmpty(condition.getEndTime()) ? condition.getEndTime() : endTime);
        Map<String, Object> orderData = merchantOrderService.getOrderCountData(params);
        if (orderData == null) {
            orderData = new HashMap<>();
        }
        params.put("userId", condition.getUserId());
        if(StringUtil.isNotEmpty(condition.getUserId())){
            params.remove("startTime");
            params.remove("endTime");
        }
        Map<String, Object> cashData = cashService.getCashCountData(params);
        orderData.putAll(cashData);
        return ResultGenerator.genSuccessResult(orderData);
    }

    /**
     * 分页查询
     */
    @RequestMapping(value = "/queryCashByPage", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryCashByPage(@RequestParam(defaultValue = "1") Integer currPage
            , @RequestParam(defaultValue = "10") Integer pageSize
            , Cash cash) {

        Map<String, Object> map = new HashMap<>(16);
        map.put("pageSize", pageSize);
        map.put("offset", (currPage - 1) * pageSize);
        if (StringUtil.isNotEmpty(cash.getType())) {
            map.put("type", cash.getType());
        }
        if (StringUtil.isNotEmpty(cash.getUserName())) {
            map.put("userName", cash.getUserName());
        }
        if (StringUtil.isNotEmpty(cash.getMerchantName())) {
            map.put("merchantName", cash.getMerchantName());
        }
        if (StringUtil.isNotEmpty(cash.getStatus())) {
            map.put("status", cash.getStatus());
        }
        if (StringUtil.isNotEmpty(cash.getPhone())) {
            map.put("phone", cash.getPhone());
        }
        if (StringUtil.isNotEmpty(cash.getId())) {
            map.put("id", cash.getId());
        }
        if (StringUtil.isNotEmpty(cash.getUserId())) {
            map.put("userId", cash.getUserId());
        }
        if (StringUtil.isNotEmpty(cash.getBeginTime())) {
            map.put("beginTime", cash.getBeginTime());
        }
        if (StringUtil.isNotEmpty(cash.getEndTime())) {
            map.put("endTime", cash.getEndTime());
        }
        List<Cash> cashList = cashService.queryCashPage(map);
        //添加手机号
        for (Cash cash2 : cashList) {
            UserInfo userInfo = new UserInfo();
            userInfo.setId(cash2.getUserId());
            userInfo = userInfoService.queryUserInfo(userInfo);
            if (userInfo != null && userInfo.getPhone() != null) {
                cash2.setPhone(userInfo.getPhone());
            }
        }
        Integer count = cashService.queryCashPageCount(map);

        Map<String, Object> result = new HashMap<>(16);
        result.put("cashList", cashList);
        result.put("count", count);
        result.put("pageCount", StringUtil.getPageCount(count, pageSize));
        result.put("currPage", currPage);
        return result;
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryCashDetails", method = RequestMethod.GET)
    public String queryCashDetails(HttpServletRequest request, Long id, Integer pageIndex) {

        if (id != null && id != 0) {
            Cash cash = new Cash();
            cash.setId(id);
            cash = cashService.queryCash(cash);
            request.setAttribute("cash", cash);
            request.setAttribute("pageIndex", pageIndex);
        }
        return "cash/cashDetails";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryCashBaseInfo", method = RequestMethod.GET)
    public String queryCashBaseInfo(HttpServletRequest request, Long id, Integer pageIndex) {

        if (id != null && id != 0) {
            Cash cash = new Cash();
            cash.setId(id);
            cash = cashService.queryCash(cash);
            request.setAttribute("cash", cash);
            request.setAttribute("pageIndex", pageIndex);
        }
        return "cash/cashBaseInfo";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryCashExtendsInfo", method = RequestMethod.GET)
    public String queryCashExtendsInfo() {
        return "cash/cashExtendsInfo";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryCashFunctionsInfo", method = RequestMethod.GET)
    public String queryCashFunctionsInfo() {
        return "cash/cashFunctionsInfo";
    }

    /**
     * 保存对象
     */
    @RequestMapping(value = "/saveCash", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveCash(Cash cash) {

        cashService.saveOrUpdate(cash);
        Map<String, Object> result = new HashMap<>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

    /**
     * 修改对象
     *
     * @throws IOException
     */
    @RequestMapping(value = "/updateCash", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateCash(Long id) {

        Cash cash = new Cash();
        cash.setId(id);
        cashService.updateCash(cash);
        Map<String, Object> result = new HashMap<>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

    /**
     * 删除对象
     *
     * @throws IOException
     */
    @RequestMapping(value = "/deleteCash", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteCash(Long id) {
        cashService.removeById(id);
        Map<String, Object> result = new HashMap<>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

    @GetMapping("/pass/{id}")
    public String pass(HttpServletRequest request, @PathVariable("id") String id) {
        Cash entity = cashService.getById(id);
        request.setAttribute("entity", entity);
        return "/cash/cashPass";
    }

    /**
     * realAuditPass:(cash提现审核通过). <br/>
     *
     * @author Administrator
     * @since JDK 1.8
     */
    @PostMapping("/realAuditPass")
    @ResponseBody
    public Result<Object> realAuditPass(Cash cash) {
        cashService.passAndRemit(cash);
        return ResultGenerator.genSuccessResult("提现成功", null);
    }

    /**
     * @description:手动通过
     * @date: 2021/9/2 19:17
     * @param cash:
     * @return cn.pluss.platform.api.Result<java.lang.Object>
     */
    @PostMapping("/auditSuccess")
    @ResponseBody
    public Result<Object> auditSuccess(Cash cash) {
        cash.setUpdateDt(new Date());
        // cash.setUpdator(queryCurrentShiroUser().getId().toString());
        cashService.updateCash(cash);
        return ResultGenerator.genSuccessResult("操作成功",null);
    }

    /**
     * realAuditPass:(cash提现审核通过). <br/>
     *
     * @author Administrator
     * @since JDK 1.8
     */
    @PostMapping("/batchPass")
    @ResponseBody
    public Result<Object> batchPass(@RequestParam(value = "ids[]", required = false) String[] ids) {
        cashService.batchPassAndRemit(Arrays.asList(ids));
        return ResultGenerator.genSuccessResult("操作成功",null);
    }

    /**
     * realauditRefuse:(cash提现审核拒绝). <br/>
     *
     * @param cash
     * @author Administrator
     * @since JDK 1.8
     */
    @PostMapping("/realauditRefuse")
    @ResponseBody
    public Map<String, Object> realauditRefuse(Cash cash) {
        Map<String, Object> result = new HashMap<>(16);
        cash.setStatus(2);
        // cash.setUpdator(queryCurrentShiroUser().getId() + "");
        cash.setUpdateDt(new Date());
        cashService.updateCash(cash);
        return result;
    }
}


