package com.iamberry.wechat.handles.withdrawal;

import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.WechatUtils;
import com.iamberry.wechat.core.entity.coupon.CouponItemDto;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.core.entity.order.Order;
import com.iamberry.wechat.core.entity.product.Product;
import com.iamberry.wechat.core.entity.welfare.UserAmount;
import com.iamberry.wechat.core.entity.welfare.UserAtLogs;
import com.iamberry.wechat.face.coupon.WelfareService;
import com.iamberry.wechat.face.member.MemberService;
import com.iamberry.wechat.face.withdrawal.UserAtLogsService;
import com.iamberry.wechat.service.mapper.UserAmountMapper;
import com.iamberry.wechat.tools.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin/atLogs")
public class AdminAtLogsController {
    @Autowired
    private UserAtLogsService userAtLogsService;
    @Autowired
    private UserAmountMapper amountMapper;
    /**
     * 查询所有提现记录
     * 2019年9月4日
     * @author LJK
     * @return
     */
    @RequestMapping("/list")
    public ModelAndView getProductList(@RequestParam(value= "pageSize",defaultValue= "15" ,required=false) Integer pageSize,
                                       @RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO,
                                       HttpServletRequest request) throws Exception {
        /*SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");*/
        StringBuffer url = new StringBuffer("/admin/admin/atLogs?pageSize=" + pageSize);
        ModelAndView mv = new ModelAndView("admin/withdrawal/atLogsList");
        String atState = request.getParameter("atState");
        String atName = request.getParameter("atName");
        String atTel = request.getParameter("atTel");
        String atType = request.getParameter("atType");
        String atIsMoney = request.getParameter("atIsMoney");
        UserAtLogs logs = new UserAtLogs();
        logs.getPage().setPageSize(pageSize);
        logs.getPage().setPageNumber(pageNO);

        if (null != atState && !"".equals(atState)) {
            logs.setAtState(Integer.valueOf(atState));
            url.append("&atState=" + atState);
        }

        if (null != atName && !"".equals(atName)) {
            url.append("&atName=" + atName);
            logs.setAtName(atName);
        }
        if (null != atTel && !"".equals(atTel)) {
            url.append("&atTel=" + atTel);
            logs.setAtTel(atTel);
        }
        if (null != atType && !"".equals(atType)) {
            url.append("&atType=" + atType);
            logs.setAtType(Integer.valueOf(atType));
        }
        if (null != atIsMoney && !"".equals(atIsMoney)) {
            url.append("&atIsMoney=" + atIsMoney);
            logs.setAtIsMoney(Integer.valueOf(atIsMoney));
        }
        List<UserAtLogs> list = userAtLogsService.selectAtlogsInfo(logs);

        for (UserAtLogs atLogs : list) {
            // 用户昵称编码
            if (atLogs.getUserNickname() != null && !atLogs.getUserNickname().isEmpty()) {
                atLogs.setUserNickname(URLDecoder.decode(atLogs.getUserNickname(), "UTF-8"));
            }
        }
        mv.addObject("list", list);

        url.append("&pageNO=");
        int count = userAtLogsService.selectAtlogsInfoCount(logs);
        int pageCount =  count % logs.getPage().getPageSize() == 0? count/logs.getPage().getPageSize():(count/logs.getPage().getPageSize())+1;
        //分页用
        mv.addObject("pageNO", pageNO);
        mv.addObject("url", url);
        mv.addObject("atState", logs.getAtState());
        mv.addObject("atName", logs.getAtName());
        mv.addObject("atTel", logs.getAtTel());
        mv.addObject("atType", logs.getAtType());
        mv.addObject("atIsMoney", logs.getAtIsMoney());
        mv.addObject("pageSize", pageCount);
        return mv;
    }
    // 返回处理结果
    public ResultMsg getSuccessResult() {
        ResultMsg remsg = new ResultMsg();
        remsg.setResultCode(ResultInfo.SUCCESSCODE);
        remsg.setMessage(ResultInfo.SUCCESSINFO);
        remsg.setStatus(true);
        return remsg;
    }
    /**
     * 修改提现记录
     * 2019年9月4日
     * @author LJK
     * @return
     */
    @ResponseBody
    @RequestMapping("/update")
    public ResultMsg update(HttpServletRequest request,UserAtLogs logs) throws Exception  {
        ResultMsg resultMsg = getSuccessResult();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(logs == null || logs.getAtId() == null){
            resultMsg.setResultCode(ResultInfo.ERRORCODE);
            resultMsg.setMessage(ResultInfo.lostMemberInfo);
            resultMsg.setStatus(false);
            return resultMsg;
        }
        /*String dateTime = request.getParameter("dateTime");
        if (null != dateTime && !"".equals(dateTime)) {
            if (!request.getMethod().equalsIgnoreCase("post")) {
                logs.setAtDealTime(format.parse(new String(dateTime.getBytes("ISO-8859-1"), "UTF-8")));
            } else {
                logs.setAtDealTime(format.parse(dateTime));
            }
        }*/
        logs.setAtDealTime(new Date());
        if(userAtLogsService.edit(logs) > 0){
            if(logs.getAtState() == 3){
                UserAtLogs userAtLogs = userAtLogsService.get(logs.getAtId());
                // 放回可提现金额、减少锁定金额
                UserAmount userAmount = amountMapper.get(userAtLogs.getUserOpenid());
                assert userAmount != null;
                userAmount.setUserOpenid(null);
                userAmount.setAmonutUsable(userAtLogs.getAtAmonut());
                userAmount.setAmonutAwait(null);
                userAmount.setAmonutLock(-userAtLogs.getAtAmonut());
                if (amountMapper.edit(userAmount) < 1) {
                    throw new Exception("修改提现失败!");
                }
            }

            return resultMsg;
        }else{
            resultMsg.setResultCode(ResultInfo.ERRORCODE);
            resultMsg.setMessage(ResultInfo.lostMemberInfo);
            resultMsg.setStatus(false);
            return resultMsg;
        }
    }

}
