package cn.pluss.platform.controller.userApp;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.converter.UserAppConverter;
import cn.pluss.platform.entity.*;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.merchantChannelStatus.MerchantChannelStatusService;
import cn.pluss.platform.systemConfig.SystemConfigService;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.util.StringUtil;
import cn.pluss.platform.vo.UserInfoVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("userApp")
public class UserAppController extends BaseNoModelController {

    private Integer PAGE_SIZE = 12;

    @Resource
    private HttpServletRequest request;

    @Autowired
    private UserAppService userAppService;

    @Autowired
    private MerchantBaseInfoService merchantBaseInfoService;

    @Autowired
    private SystemConfigService systemConfigService;

    @Setter(onMethod_ = {@Lazy, @Autowired})
    private MerchantChannelStatusService merchantChannelStatusService;

    /**
     * 引导页
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        String userType = request.getParameter("userType");
        request.setAttribute("userType", userType);
        return "userApp/userAppList";
    }

    /**
     * 分页查询
     */
    @RequestMapping(value = "/queryUserAppByPage", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryUserAppByPage(@RequestParam(defaultValue = "1") Integer currPage, UserApp userApp) {
        Map<String, Object> result = new HashMap<>(16);
        Map<String, Object> map = new HashMap<>(16);
        // ShiroUser queryCurrentShiroUser = queryCurrentShiroUser();
        map.put("pageSize", PAGE_SIZE);
        map.put("offset", (currPage - 1) * PAGE_SIZE);
        map.put("userType", userApp.getUserType());
        Page<UserInfoVO> pageData = userAppService.queryUserInfoVOPage(map);
        Long count = pageData.getTotal();
        result.put("userAppList", pageData.getRecords());
        result.put("count", count);
        result.put("pageCount", StringUtil.getPageCount(count.intValue(), PAGE_SIZE));
        result.put("currPage", currPage);
        return result;
    }

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

        if (id != null && id != 0) {
            UserApp userApp = new UserApp();
            userApp.setId(id);
            userApp = userAppService.queryUserApp(userApp);
            request.setAttribute("userApp", userApp);
            request.setAttribute("pageIndex", pageIndex);
        }
        return "userApp/userAppDetails";
    }

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

        if (id != null && id != 0) {
            UserApp userApp = new UserApp();
            userApp.setId(id);
            userApp = userAppService.queryUserApp(userApp);
            request.setAttribute("userApp", userApp);
            request.setAttribute("pageIndex", pageIndex);
        }
        return "userApp/userAppBaseInfo";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryUserAppExtendsInfo", method = RequestMethod.GET)
    public String queryUserAppExtendsInfo() {

        return "userApp/userAppExtendsInfo";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryUserAppFunctionsInfo", method = RequestMethod.GET)
    public String queryUserAppFunctionsInfo() {

        return "userApp/userAppFunctionsInfo";
    }

    /**
     * 保存对象
     *
     * @throws IOException
     */
    @PostMapping(value = "/saveUserApp")
    @ResponseBody
    public Map<String, Object> saveUserApp(UserApp userApp) {
        userAppService.saveOrUpdate(userApp);
        Map<String, Object> result = new HashMap<>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

    /**
     * 保存对象
     *
     * @throws IOException
     */
    @PostMapping(value = "/updateByUserId")
    @ResponseBody
    public Map<String, Object> updateByUserId(UserApp userApp) {
        UpdateWrapper<UserApp> uWrapper = new UpdateWrapper<>();
        uWrapper.eq("userId", userApp.getUserId());
        userAppService.update(userApp, uWrapper);
        Map<String, Object> result = new HashMap<>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

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

    /**
     * goAisleSwitch:(跳转到切换通道页面). <br/>
     *
     * @return
     * 
     * @author Administrator
     * @since JDK 1.8
     */
    @GetMapping(value = "/goAisleSwitchPage")
    public String goAisleSwitch() {
        String aisleSwitch = systemConfigService.querySystemConfigValue("aisleSwitch");
        request.setAttribute("aisleSwitch", aisleSwitch);
        if ("1".equals(aisleSwitch)) {
            request.setAttribute("aisleSwitchValue", "乐刷通道");
        } else if ("0".equals(aisleSwitch)) {
            request.setAttribute("aisleSwitchValue", "支付宝，微信官方通道");//支付宝，微信官方通道
        }else if("2".equals(aisleSwitch)){
            request.setAttribute("aisleSwitchValue", "随行付");//支付宝，微信官方通道
        }
        return "userApp/goAisleSwitchPage";
    }

//    /**
//     * 
//     * aisleSwitchDetail:(加载当前用户的通道信息).<br/> 
//     * 
//     * @author Administrator 
//     * @return 
//     * @since JDK 1.8
//     */
//    @PostMapping(value="/aisleSwitchDetail")
//    @ResponseBody
//    public Map<String, Object> aisleSwitchDetail(){
//    	Map<String, Object> result=new HashMap<String, Object>(16);
//    	ShiroUser shiroUser = queryCurrentShiroUser();
//    	UserApp userApp=new UserApp();
//    	if(null!=shiroUser.getId()) {
//    		userApp.setUserId(shiroUser.getId().intValue());
//    		List<UserApp> queryUserAppList = userAppService.queryUserAppList(userApp);
//    		if(queryUserAppList!=null&&queryUserAppList.size()>0) {
//    			userApp=queryUserAppList.get(0);
//    			result.put("code", "1");//成功
//    			result.put("userApp", userApp);
//    			return result;
//    		}else {
//    			result.put("code", "0");
//    			return result;
//    		}
//    	}
//    	result.put("code", "0");
//		return result;
//    }

    @PostMapping("/updateAisleSwitch")
    @ResponseBody
    public Map<String, Object> updateAisleSwitch(String aisleSwitch) {
        Map<String, Object> result = new HashMap<>(16);
        try {
            SystemConfig systemConfig = new SystemConfig();
            systemConfig.setPropertyKey("aisleSwitch");
            SystemConfig querySystemConfig = systemConfigService.querySystemConfig(systemConfig);
            querySystemConfig.setPropertyValue(aisleSwitch);
            systemConfigService.updateSystemConfig(querySystemConfig);
            result.put("aisleSwitchValue", aisleSwitch);
            result.put("code", "1");
        } catch (NumberFormatException e) {
            e.printStackTrace();
            result.put("code", "0");
        }
        return result;
    }

    @GetMapping("/getByUId/{userIds}")
    @ResponseBody
    public Result<Map<String, Object>> getUserByUid(@PathVariable("userIds") String userIds) {
        final Map<String, Object> resultMap = userAppService.getUserByUid(userIds);
        return ResultGenerator.genSuccessResult(resultMap);
    }

    @PostMapping("/closeSpreadPerm")
    @ResponseBody
    public Result<Object> closeSpreadPerm(String userId) {
        UserInfo ui = uiService.getById(userId);
        ui.setStatus(UserInfo.STATUS_NO_SPREAD);
        uiService.updateById(ui);

        return ResultGenerator.genSuccessResult("操作成功", null);
    }

    @PostMapping("/openSpreadPerm")
    @ResponseBody
    public Result<Object> openSpreadPerm(String userId) {
        UserInfo ui = uiService.getById(userId);
        ui.setStatus(UserInfo.STATUS_VALID);
        uiService.updateById(ui);

        return ResultGenerator.genSuccessResult("操作成功", null);
    }
}
