package cn.pluss.platform.controller.activityConsumReturn;

import cn.pluss.platform.activityConsumReturn.ActivityConsumReturnService;
import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.entitiy.ShiroUser;
import cn.pluss.platform.entity.ActivityConsumReturn;
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("activityConsumReturn")
public class ActivityConsumReturnController extends BaseNoModelController {

    private Integer PAGE_SIZE = 12;

    @Autowired
    private ActivityConsumReturnService activityConsumReturnService;

    @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 "activityConsumReturn/activityConsumReturnList";
    }

    /**
     * 分页查询
     */
    @RequestMapping(value = "/queryActivityConsumReturnByPage", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryActivityConsumReturnByPage(@RequestParam(defaultValue = "1") Integer currPage, ActivityConsumReturn activityConsumReturn) {

        Map<String, Object> result = new HashMap<String, Object>(16);
        Map<String, Object> map = new HashMap<String, Object>(16);
        map.put("pageSize", PAGE_SIZE);
        map.put("offset", (currPage - 1) * PAGE_SIZE);
        map.put("startDt", activityConsumReturn.getStartDt());
        map.put("endDt", activityConsumReturn.getEndDt());
        map.put("name", activityConsumReturn.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<ActivityConsumReturn> activityConsumReturnList = null;
		Integer count = 0;
        if (bol){
			activityConsumReturnList = activityConsumReturnService.queryActivityConsumReturnPage(map);
			count = activityConsumReturnService.queryActivityConsumReturnPageCount(map);
		}else {
			activityConsumReturnList = activityConsumReturnService.queryActivityNoConsumReturnPage(map);
			count = activityConsumReturnService.queryActivityNoConsumReturnPageCount(map);
		}
        result.put("activityConsumReturnList", activityConsumReturnList);
        result.put("count", count);
        result.put("pageCount", StringUtil.getPageCount(count, PAGE_SIZE));
        result.put("currPage", currPage);
        return result;
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryActivityConsumReturnDetails", method = RequestMethod.GET)
    public String queryActivityConsumReturnDetails(HttpServletRequest request, Integer id, Integer pageIndex) {
        if (id != null && id != 0) {
            ActivityConsumReturn activityConsumReturn = new ActivityConsumReturn();
            activityConsumReturn.setId(id);
            activityConsumReturn = activityConsumReturnService.queryActivityConsumReturn(activityConsumReturn);
            request.setAttribute("activityConsumReturn", activityConsumReturn);
            request.setAttribute("pageIndex", pageIndex);
        }
        return "activityConsumReturn/activityConsumReturnDetails";
    }

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

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryActivityConsumReturnBaseInfo", method = RequestMethod.GET)
    public String queryActivityConsumReturnBaseInfo(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) {
            ActivityConsumReturn activityConsumReturn = new ActivityConsumReturn();
            activityConsumReturn.setId(id);
            activityConsumReturn = activityConsumReturnService.queryActivityConsumReturn(activityConsumReturn);
            request.setAttribute("activityConsumReturn", activityConsumReturn);
            request.setAttribute("pageIndex", pageIndex);
            //如果是管理员
            if (bol) {
                MerchantStore merchantStore = new MerchantStore();
                merchantStore.setMerchantCode(activityConsumReturn.getMerchantCode());
                List<MerchantStore> merchantStoreList = merchantStoreService.queryMerchantStoreList(merchantStore);
                request.setAttribute("merchantStoreList", merchantStoreList);
            }
        }
        getRole();
        getPermission();
        getMerchant();
        return "activityConsumReturn/activityConsumReturnBaseInfo";
    }

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

        return "activityConsumReturn/activityConsumReturnExtendsInfo";
    }

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

        return "activityConsumReturn/activityConsumReturnFunctionsInfo";
    }

    /**
     * 保存对象
     *
     * @throws IOException
     */
    @RequestMapping(value = "/saveActivityConsumReturn", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveActivityConsumReturn(ActivityConsumReturn activityConsumReturn) {
        //通过merchantCode查商户信息
        MerchantBaseInfo merchantBaseInfo = new MerchantBaseInfo();
        if (StringUtil.isNotEmpty(activityConsumReturn.getMerchantCode())){
            merchantBaseInfo.setMerchantCode(activityConsumReturn.getMerchantCode());
        }else {
            ShiroUser user=queryCurrentShiroUser();
            activityConsumReturn.setMerchantCode(user.getMerchantCode());
            merchantBaseInfo.setMerchantCode(user.getMerchantCode());
        }
        List<MerchantBaseInfo> merchantBaseInfoList = merchantBaseInfoService.queryMerchantBaseInfoList(merchantBaseInfo);
        if (merchantBaseInfoList != null && merchantBaseInfoList.size() > 0) {
            activityConsumReturn.setMerchantName(merchantBaseInfoList.get(0).getMerchantName());
        }
        //通过StoreId查门店信息
        MerchantStore merchantStore = new MerchantStore();
        merchantStore.setStoreId(activityConsumReturn.getStoreId());
        List<MerchantStore> merchantStoreList = merchantStoreService.queryMerchantStoreList(merchantStore);
        if (merchantStoreList != null && merchantStoreList.size() > 0) {
            activityConsumReturn.setStoreName(merchantStoreList.get(0).getStoreName());
        }
        if (activityConsumReturn.getId() != null && activityConsumReturn.getId() != 0) {
            if (StringUtil.isNotEmpty(activityConsumReturn)) {
                if (null != activityConsumReturn.getTimeType() && 0 == activityConsumReturn.getTimeType()) {
                    activityConsumReturn.setStartDt(null);
                    activityConsumReturn.setEndDt(null);
                }
            }
            activityConsumReturnService.updateActivityConsumReturn(activityConsumReturn);
        } else {
            ActivityConsumReturn aConsumReturn = new ActivityConsumReturn();
            aConsumReturn.setMerchantCode(activityConsumReturn.getMerchantCode());
            aConsumReturn.setStoreId(activityConsumReturn.getStoreId());
            List<ActivityConsumReturn> queryActivityConsumReturnList = activityConsumReturnService.queryActivityConsumReturnList(aConsumReturn);
            for (ActivityConsumReturn a : queryActivityConsumReturnList) {
                activityConsumReturnService.deleteActivityConsumReturn(a);
            }
            if (StringUtil.isEmpty(activityConsumReturn.getUserId())) {
                ShiroUser shiroUser = queryCurrentShiroUser();
                if (StringUtil.isNotEmpty(shiroUser)) {
                    activityConsumReturn.setUserId(shiroUser.getId().intValue());
                }
            }
            activityConsumReturnService.saveActivityConsumReturn(activityConsumReturn);
        }
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

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

        ActivityConsumReturn activityConsumReturn = new ActivityConsumReturn();
        activityConsumReturn.setId(id);
        activityConsumReturnService.updateActivityConsumReturn(activityConsumReturn);
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

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

    /**
     * 管理员权限标识
     */
    public void getRole() {
        Boolean bol = verifyUserRole();
        if (bol) {
            request.setAttribute("admin", "admin");
        }
    }

    /**
     * 不是管理员 获取的权限  是商户
     */
    public void getPermission() {
        ShiroUser user = queryCurrentShiroUser();
        if (StringUtil.isNotEmpty(user.getMerchantCode())) {
            request.setAttribute("merchant", "merchant");
        }
    }

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

}


