package com.xmomen.module.base.controller;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
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.RestController;

import com.xmomen.framework.mybatis.dao.MybatisDao;
import com.xmomen.framework.mybatis.page.Page;
import com.xmomen.framework.utils.AssertExt;
import com.xmomen.framework.web.exceptions.ArgumentValidException;
import com.xmomen.module.base.constant.AppConstants;
import com.xmomen.module.base.entity.CdActivityAddress;
import com.xmomen.module.base.entity.CdCoupon;
import com.xmomen.module.base.entity.CdCouponRef;
import com.xmomen.module.base.entity.CdCouponRefExample;
import com.xmomen.module.base.mapper.CouponMapper;
import com.xmomen.module.base.mapper.ItemMapper;
import com.xmomen.module.base.model.CouponActivityAddress;
import com.xmomen.module.base.model.CouponModel;
import com.xmomen.module.base.model.CouponQuery;
import com.xmomen.module.base.model.CreateCoupon;
import com.xmomen.module.base.model.ItemChildModel;
import com.xmomen.module.base.model.ReadCardVo;
import com.xmomen.module.base.model.UpdateCoupon;
import com.xmomen.module.base.service.CouponService;
import com.xmomen.module.logger.Log;

@RestController
public class CouponActivityController {

    @Autowired
    CouponService couponService;

    @Autowired
    MybatisDao mybatisDao;

    // 卡券列表
    @RequestMapping(value = "/couponActivity", method = RequestMethod.GET)
    @Log(actionName = "卡劵回填查询")
    public Page<CouponModel> getCouponList(@RequestParam(value = "limit") Integer limit,
                                  @RequestParam(value = "offset") Integer offset,
                                  @RequestParam(value = "couponNumber", required = false) String couponNumber,
                                  @RequestParam(value = "couponType",required = false) Integer couponType,
                                  @RequestParam(value = "couponCategoryId",required = false)Integer couponCategoryId,
                                  @RequestParam(value = "isSend",required = false) Integer isSend,
                                  @RequestParam(value = "cdCompanyId",required = false) Integer cdCompanyId,
                                  @RequestParam(value = "customerMangerId",required = false) Integer customerMangerId,
                                  @RequestParam(value = "isUseful",required = false) Integer isUseful,
                                  @RequestParam(value = "isOver",required = false) Integer isOver,
                                  @RequestParam(value = "batch",required = false) String batch,
                                  @RequestParam(value = "keyword", required = false) String keyword){
        // 创建卡券查询对象
        CouponQuery couponQuery = new CouponQuery();
        // 设置查询关键字
        couponQuery.setKeyword(keyword);
        // 设置公司ID
        couponQuery.setCdCompanyId(cdCompanyId);
        // 设置卡券分类ID
        couponQuery.setCouponCategoryId(couponCategoryId);
        // 设置卡券编号
        couponQuery.setCouponNumber(couponNumber);
        // 设置卡券类型
        couponQuery.setCouponType(couponType);
        // 设置客户经理ID
        couponQuery.setCustomerMangerId(customerMangerId);
        // 设置是否过期
        couponQuery.setIsOver(isOver);
        // 设置是否发送
        couponQuery.setIsSend(isSend);
        // 设置是否有效
        couponQuery.setIsUseful(isUseful);
        // 设置批次
        if(!StringUtils.isBlank(batch)){
            couponQuery.setBatch(batch);
        }
        //客服经理过滤 如果有客服组权限则不过滤
        if(SecurityUtils.getSubject().hasRole(AppConstants.CUSTOMER_MANAGER_PERMISSION_CODE) && !SecurityUtils.getSubject().hasRole(AppConstants.CUSTOMER_PERMISSION_CODE)){
            // 获取当前用户ID
            Integer userId = (Integer) SecurityUtils.getSubject().getSession().getAttribute(AppConstants.SESSION_USER_ID_KEY);
            // 设置客服经理ID
            couponQuery.setManagerId(userId);
        }
        // 查询卡券列表
        return couponService.queryCouponActivity(couponQuery, limit, offset);
    }

    // 卡券列表
    @RequestMapping(value = "/couponActivity/couponActivityAddress", method = RequestMethod.GET)
    @Log(actionName = "参与活动卡劵回填查询")
    public Page<CouponModel> getCouponActivityAddressList(@RequestParam(value = "limit") Integer limit,
                                  @RequestParam(value = "offset") Integer offset,
                                  @RequestParam(value = "couponNumber", required = false) String couponNumber,
                                  @RequestParam(value = "couponType",required = false) Integer couponType,
                                  @RequestParam(value = "couponCategoryId",required = false)Integer couponCategoryId,
                                  @RequestParam(value = "isSend",required = false) Integer isSend,
                                  @RequestParam(value = "cdCompanyId",required = false) Integer cdCompanyId,
                                  @RequestParam(value = "customerMangerId",required = false) Integer customerMangerId,
                                  @RequestParam(value = "isUseful",required = false) Integer isUseful,
                                  @RequestParam(value = "isOver",required = false) Integer isOver,
                                  @RequestParam(value = "batch",required = false) String batch,
                                  @RequestParam(value = "keyword", required = false) String keyword){
        // 创建卡券查询对象
        CouponQuery couponQuery = new CouponQuery();
        // 设置查询关键字
        couponQuery.setKeyword(keyword);
        // 设置公司ID
        couponQuery.setCdCompanyId(cdCompanyId);
        // 设置卡券分类ID
        couponQuery.setCouponCategoryId(couponCategoryId);
        // 设置卡券编号
        couponQuery.setCouponNumber(couponNumber);
        // 设置卡券类型
        couponQuery.setCouponType(couponType);
        // 设置客户经理ID
        couponQuery.setCustomerMangerId(customerMangerId);
        // 设置是否过期
        couponQuery.setIsOver(isOver);
        // 设置是否发送
        couponQuery.setIsSend(isSend);
        // 设置是否有效
        couponQuery.setIsUseful(isUseful);
        // 设置批次
        if(!StringUtils.isBlank(batch)){
            couponQuery.setBatch(batch);
        }
        //客服经理过滤 如果有客服组权限则不过滤
        if(SecurityUtils.getSubject().hasRole(AppConstants.CUSTOMER_MANAGER_PERMISSION_CODE) && !SecurityUtils.getSubject().hasRole(AppConstants.CUSTOMER_PERMISSION_CODE)){
            // 获取当前用户ID
            Integer userId = (Integer) SecurityUtils.getSubject().getSession().getAttribute(AppConstants.SESSION_USER_ID_KEY);
            // 设置客服经理ID
            couponQuery.setManagerId(userId);
        }
        // 查询卡券列表
        return (Page<CouponModel>) mybatisDao.selectPage(CouponMapper.CouponMapperNameSpace + "getCouponActivityAddressList", couponQuery, limit, offset);
    }
    
    
    // 查询卡填写的活动送货地址
    @RequestMapping(value = "/couponActivity/getCouponActivityAddress", method = RequestMethod.GET)
	public List<CouponActivityAddress> getCouponActivityAddress(@RequestParam(value = "couponNumber", required = false) String couponNumber){
    	// 创建卡券活动地址列表
    	List<CouponActivityAddress> couponActivityAddressList = new ArrayList<CouponActivityAddress>();
    	// 查询卡券活动地址
    	couponActivityAddressList = mybatisDao.getSqlSessionTemplate().selectList(CouponMapper.CouponMapperNameSpace + "getActivityAddress", couponNumber);
		return couponActivityAddressList;
	}
}
