package cn.pluss.platform.controller.activityRecharge;

import cn.pluss.platform.activityRecharge.ActivityRechargeService;
import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.entitiy.ShiroUser;
import cn.pluss.platform.entity.ActivityRecharge;
import cn.pluss.platform.entity.MerchantBaseInfo;
import cn.pluss.platform.entity.MerchantStore;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.merchantStore.MerchantStoreService;
import cn.pluss.platform.util.StringUtil;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

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

/**
 * @author Administrator
 */
@Controller
@RequestMapping("activityRecharge")
public class ActivityRechargeController extends BaseNoModelController {

    private Integer PAGE_SIZE = 12;

    @Autowired
    private ActivityRechargeService activityRechargeService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private MerchantBaseInfoService merchantBaseInfoService;

    @Autowired
    private MerchantStoreService merchantStoreService;

    /**
     * 引导页
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        getRole();
        getPermission();
        return "activityRecharge/activityRechargeList";
    }

    /**
     * 分页查询
     */
    @RequestMapping(value = "/queryActivityRechargeByPage", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryActivityRechargeByPage(@RequestParam(defaultValue = "1") Integer currPage, ActivityRecharge activityRecharge) {
        Map<String, Object> map = new HashMap<String, Object>(16);
        Map<String, Object> result = new HashMap<String, Object>(16);
        map.put("pageSize", PAGE_SIZE);
        map.put("offset", (currPage - 1) * PAGE_SIZE);
        if (null != activityRecharge.getStartDt()) {
            map.put("startDt", activityRecharge.getStartDt());
        }
        if (null != activityRecharge.getEndDt()) {
            map.put("endDt", activityRecharge.getEndDt());
        }
        if (null != activityRecharge.getId()) {
            map.put("id", activityRecharge.getId());
        }
        if (null != activityRecharge.getMerchantName() && !"".equals(activityRecharge.getMerchantName())) {
            map.put("merchantName", activityRecharge.getMerchantName());
        }
        //map.put("name", activityRecharge.getName());
        Boolean bol = verifyUserRole();
        //是否是管理员进行数据筛选和权限标记
        if (bol) {
            result.put("admin", "admin");
        } else {
            ShiroUser shiroUser = queryCurrentShiroUser();
            if (StringUtil.isNotEmpty(shiroUser.getMerchantCode())) {
                result.put("merchant", "merchant");
                map.put("merchantCode", shiroUser.getMerchantCode());
            } else {
                map.put("merchantCode", StringUtil.genRandomNum(33));
            }
        }
        List<ActivityRecharge> activityRechargeList = null;
        Integer count = 0;
        if (!bol) {
            activityRechargeList = activityRechargeService.queryActivityNoRechargePage(map);
            count = activityRechargeService.queryActivityNoRechargePageCount(map);
        } else {
            activityRechargeList = activityRechargeService.queryActivityRechargePage(map);
            count = activityRechargeService.queryActivityRechargePageCount(map);
        }
        result.put("activityRechargeList", activityRechargeList);
        result.put("count", count);
        result.put("pageCount", StringUtil.getPageCount(count, PAGE_SIZE));
        result.put("currPage", currPage);
        return result;
    }

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

        if (id != null && id != 0) {
            ActivityRecharge activityRecharge = new ActivityRecharge();
            activityRecharge.setId(id);
            activityRecharge = activityRechargeService.queryActivityRecharge(activityRecharge);
            request.setAttribute("activityRecharge", activityRecharge);
            request.setAttribute("pageIndex", pageIndex);
        }
        return "activityRecharge/activityRechargeDetails";
    }

    /**
     * 其他用户查询详情
     */
    @RequestMapping(value = "/otherActivityRechargeDetails", method = RequestMethod.GET)
    public String otherActivityRechargeDetails(HttpServletRequest request, Integer id, Integer pageIndex) {
        if (id != null && id != 0) {
            ActivityRecharge activityRecharge = new ActivityRecharge();
            activityRecharge.setId(id);
            activityRecharge = activityRechargeService.queryActivityRecharge(activityRecharge);
            request.setAttribute("activityRecharge", activityRecharge);
            request.setAttribute("pageIndex", pageIndex);
        }
        return "activityRecharge/otherActivityRechargeDetails";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryActivityRechargeBaseInfo", method = RequestMethod.GET)
    public String queryActivityRechargeBaseInfo(HttpServletRequest request, Integer id, Integer pageIndex) {
        Boolean bol = verifyUserRole();
        if (!bol) {
            ShiroUser user = queryCurrentShiroUser();
            if (StringUtil.isNotEmpty(user.getMerchantCode())) {
                MerchantStore merchantStore = new MerchantStore();
                merchantStore.setMerchantCode(user.getMerchantCode());
				List<MerchantStore> merchantStoreList = merchantStoreService.queryMerchantNoStoreList(merchantStore);
                request.setAttribute("merchantStoreList", merchantStoreList);
            }
        }
        if (id != null && id != 0) {
            ActivityRecharge activityRecharge = new ActivityRecharge();
            activityRecharge.setId(id);
            activityRecharge = activityRechargeService.queryActivityRecharge(activityRecharge);
            request.setAttribute("activityRecharge", activityRecharge);
            request.setAttribute("pageIndex", pageIndex);
            //如果是管理员
            if (bol) {
                MerchantStore merchantStore = new MerchantStore();
                merchantStore.setMerchantCode(activityRecharge.getMerchantCode());
                List<MerchantStore> merchantStoreList = merchantStoreService.queryMerchantStoreList(merchantStore);
                request.setAttribute("merchantStoreList", merchantStoreList);
            }
        }
        getRole();
        getPermission();
        getMerchant();
        return "activityRecharge/activityRechargeBaseInfo";
    }

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

        return "activityRecharge/activityRechargeExtendsInfo";
    }

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

        return "activityRecharge/activityRechargeFunctionsInfo";
    }

    /**
     * 保存对象
     *
     * @throws IOException
     */
    @RequestMapping(value = "/saveActivityRecharge", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveActivityRecharge(ActivityRecharge activityRecharge) {

        //通过merchantCode查商户信息
        MerchantBaseInfo merchantBaseInfo = new MerchantBaseInfo();
        if (StringUtil.isNotEmpty(activityRecharge.getMerchantCode())) {
			merchantBaseInfo.setMerchantCode(activityRecharge.getMerchantCode());
        }else {
            ShiroUser user = queryCurrentShiroUser();
            activityRecharge.setMerchantCode(user.getMerchantCode());
            merchantBaseInfo.setMerchantCode(user.getMerchantCode());
		}
		List<MerchantBaseInfo> merchantBaseInfoList = merchantBaseInfoService.queryMerchantBaseInfoList(merchantBaseInfo);
		if (merchantBaseInfoList != null && merchantBaseInfoList.size() > 0) {
			activityRecharge.setMerchantName(merchantBaseInfoList.get(0).getMerchantName());
		}
        //通过StoreId查门店信息
        MerchantStore merchantStore = new MerchantStore();
        merchantStore.setStoreId(activityRecharge.getStoreId());
        if (StringUtil.isNotEmpty(activityRecharge.getStoreId())) {
            List<MerchantStore> merchantStoreList = merchantStoreService.queryMerchantStoreList(merchantStore);
            if (merchantStoreList != null && merchantStoreList.size() > 0) {
                activityRecharge.setStoreName(merchantStoreList.get(0).getStoreName());
            }
        }
        if (activityRecharge.getId() != null && activityRecharge.getId() != 0) {
            if (StringUtil.isNotEmpty(activityRecharge)) {
                if (null != activityRecharge.getTimeType() && 0 == activityRecharge.getTimeType()) {
                    activityRecharge.setStartDt(null);
                    activityRecharge.setEndDt(null);
                }
            }
            activityRechargeService.updateActivityRecharge(activityRecharge);
        } else {
            if (StringUtil.isEmpty(activityRecharge.getUserId())) {
                ShiroUser shiroUser = queryCurrentShiroUser();
                if (StringUtil.isNotEmpty(shiroUser)) {
                    activityRecharge.setUserId(shiroUser.getId().intValue());
                }
            }
            ActivityRecharge aRecharge = new ActivityRecharge();
            aRecharge.setMerchantCode(activityRecharge.getMerchantCode());
            aRecharge.setStoreId(activityRecharge.getStoreId());
            List<ActivityRecharge> queryActivityRechargeList = activityRechargeService.queryActivityRechargeList(aRecharge);
            for (ActivityRecharge a : queryActivityRechargeList) {
                activityRechargeService.deleteActivityRecharge(a);
            }
            activityRechargeService.saveActivityRecharge(activityRecharge);
        }
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

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

        ActivityRecharge activityRecharge = new ActivityRecharge();
        activityRecharge.setId(id);
        activityRechargeService.updateActivityRecharge(activityRecharge);
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

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

    /**
     * 获取所有商户信息
     */
    public void getMerchant() {
        List<MerchantBaseInfo> merchantBaseInfoList = merchantBaseInfoService.queryMerchantBaseInfoList(null);
        request.setAttribute("merchantBaseInfoList", merchantBaseInfoList);
    }

}


