package com.chaozhanggui.admin.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.chaozhanggui.admin.system.annotation.OpLog;
import com.chaozhanggui.admin.system.model.CreateAgencyReq;
import com.chaozhanggui.admin.system.service.AgencyService;
import com.chaozhanggui.admin.system.util.MD5Util;
import com.chaozhanggui.admin.system.util.SignUtils;
import com.chaozhanggui.common.system.config.RespBody;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.*;

/**
 * 机构管理
 */
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("agency")
@Slf4j
public class AgencyController {


    @Autowired
    AgencyService agencyService;

    /**
     * 创建机构
     * @param loginName 登录名称
     * @param token 登录token
     * @param userId 用户id
     * @param agencyReq 机构信息
     * @return
     */
    @OpLog(opName = "创建机构", opDetail = "创建机构", opCode = "AGENCY_ADD")
    @RequestMapping("addAgency")
    public RespBody addAgency(@RequestHeader("loginName") String loginName, @RequestHeader("token") String token, @RequestHeader("userId") String userId,
                              @RequestBody @Valid CreateAgencyReq agencyReq
                              ){
        try {
            return agencyService.addAgency(userId,agencyReq);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new RespBody("999997");
    }

    /**
     * 机构列表
     * @param loginName 登录名称
     * @param token token
     * @param userId 用户id
     * @param agencyCode 机构代码
     * @param pageNum 开始条数
     * @param pageSize 最终条数
     * @return
     */
    @GetMapping(value = "queryAgency")
    public RespBody queryAgency(@RequestHeader("loginName") String loginName, @RequestHeader("token") String token, @RequestHeader("userId") String userId,
                                @RequestParam("agencyCode") String agencyCode,@RequestParam("pageNum") Integer pageNum,@RequestParam("pageSzie") Integer pageSize,
                               @RequestParam("userType") String userType,@RequestParam("isExtend") String isExtend
                                ){
        return agencyService.queryAgency(userId,userType,agencyCode,isExtend,pageNum,pageSize);
    }


    /**
     * 获取商户列表
     * @param loginName
     * @param token
     * @param userId
     * @param agencyCode
     * @param pageNum
     * @param pageSize
     * @param userType
     * @param isExtend
     * @return
     */
    @GetMapping(value = "queryCustormerFlow")
    public RespBody queryCustormerFlow(@RequestHeader("loginName") String loginName, @RequestHeader("token") String token, @RequestHeader("userId") String userId,
                                       @RequestParam("agencyCode") String agencyCode,@RequestParam("pageNum") Integer pageNum,@RequestParam("pageSzie") Integer pageSize,
                                       @RequestParam("userType") String userType,@RequestParam("isExtend") String isExtend,
                                       @RequestParam("merchantCode") String merchantCode,@RequestParam("status") String status,
                                       @RequestParam("merchantType") String merchantType,@RequestParam("reqUserId") String reqUserId,
                                       @RequestParam("phone") String phone
    ){
        return agencyService.queryCustormFlow(userId, userType, agencyCode, isExtend,merchantCode,status,merchantType,reqUserId, pageNum, pageSize,phone);
    }

    /**
     * 商户列表数据统计
     * @param loginName
     * @param token
     * @param userId
     * @return
     */
    @GetMapping("queryCustormSum")
    public RespBody queryCustormSum(@RequestHeader("loginName") String loginName, @RequestHeader("token") String token, @RequestHeader("userId") String userId){
        return agencyService.queryCustormSum(userId);
    }
    /**
     * 修改下级费率
     * @param loginName 登录名称
     * @param token token
     * @param userId 用id
     * @param id 修改id
     * @param fee 下级费率值
     * @return
     */
    @OpLog(opName = "修改费率", opDetail = "修改费率", opCode = "UPDATE—FEE")
    @RequestMapping("modifyFee")
    public RespBody modifyFee(@RequestHeader("loginName") String loginName, @RequestHeader("token") String token, @RequestHeader("userId") String userId
        , @RequestParam("id") Integer id, @RequestParam("fee") BigDecimal fee
                              ){
        return agencyService.modifyFee(userId,id,fee);
    }


    /**
     * 当前用户设备总数
     * @param loginName
     * @param token
     * @param userId
     * @return
     */
    @RequestMapping("getSumDeviceStock")
    public RespBody getSumDeviceStock(@RequestHeader("loginName") String loginName, @RequestHeader("token") String token, @RequestHeader("userId") String userId){
        return agencyService.getSumDeviceStock(userId);
    }

    /**
     * 获取设备列表
     * @param loginName
     * @param token
     * @param userId
     * @param merchantCode
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("getDeviceStockInfo")
    public RespBody getDeviceStockInfo(@RequestHeader("loginName") String loginName, @RequestHeader("token") String token, @RequestHeader("userId") String userId,
                                       @RequestParam("merchantCode") String merchantCode,@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,@RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize
                                       ){
        return agencyService.getSumDeviceStockInfo(userId,merchantCode,pageNum,pageSize);
    }


    /**
     * 获取创客审核列表
     * @param loginName
     * @param token
     * @param userId
     * @param phone 可以为空
     * @param name 可以为空
     * @return
     */
    @GetMapping(value = "getUserMark")
    public RespBody getUserMark(@RequestHeader("loginName") String loginName, @RequestHeader("token") String token, @RequestHeader("userId") String userId
    ,@RequestParam("phone") String phone,@RequestParam("name") String name,@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,@RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize

    ){
        return agencyService.getUserMark(userId, phone, name, pageNum, pageSize);
    }

    /**
     * 审核创客申请审核
     * @param loginName
     * @param token
     * @param userId
     * @param id 用户id
     * @param status 状态 1 通过 2 驳回
     * @param  remark 驳回备注
     * @return
     */
    @OpLog(opName = "审核创客申请审核", opDetail = "审核创客申请审核", opCode = "CUSTOMER-APPLY")
    @GetMapping("updateUserMark")
    public RespBody moddifyMark(@RequestHeader("loginName") String loginName, @RequestHeader("token") String token, @RequestHeader("userId") String userId,
                                @RequestParam("id") Integer id, @RequestParam("status") String status,@RequestParam("remark") String remark
                                ){
        return agencyService.updateUserMark(id,status,remark);
    }


    /**
     * 获取收益信息
     * @param loginName
     * @param token
     * @param userId
     * @param reqUserId 上级id 必传
     * @param orderNumber 订单号 可以为空
     * @param merchantCode 商户号 可以为空
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "queryProfit")
    public RespBody queryProfit(@RequestHeader("loginName") String loginName, @RequestHeader("token") String token, @RequestHeader("userId") String userId
                                ,@RequestParam("reqUserId") String reqUserId,@RequestParam("orderNumber") String orderNumber,@RequestParam("merchantCode") String merchantCode,@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,@RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize
    ){
        return agencyService.queryProfit(reqUserId,orderNumber,merchantCode,pageNum,pageSize);
    }


    /**
     * 查询订单信息
     * @param loginName
     * @param token
     * @param userId
     * @param reqUserId
     * @param orderNumber
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("queryOrder")
    public RespBody queryOrder(@RequestHeader("loginName") String loginName, @RequestHeader("token") String token, @RequestHeader("userId") String userId
            ,@RequestParam("reqUserId") String reqUserId,@RequestParam("orderNumber") String orderNumber,@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,@RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        return agencyService.queryOrder(reqUserId,orderNumber,pageNum,pageSize);
    }



    @GetMapping("mytest")
    public RespBody mytest(@RequestParam("userId") String userId){
        Map<String,Object> map=new HashMap<>();
        map.put("orderId","wk0002156");
        map.put("cashStatus",agencyService.getCashMap(userId,new LinkedList<>()));
        return new RespBody("000000",map);
    }



}
