package com.ivchat.park.coupons.action;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import net.sf.json.JSONObject;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.ivchat.common.action.BaseAction;
import com.ivchat.common.enums.Constant;
import com.ivchat.common.util.AjaxJsonUtil;
import com.ivchat.common.util.dg.FlexGridExtUtil;
import com.ivchat.common.util.ui.AjaxFormResponseUtil;
import com.ivchat.common.vo.DataGridPage;
import com.ivchat.common.vo.PageBean;
import com.ivchat.common.vo.ValidateBean;
import com.ivchat.park.common.model.ACLUser;
import com.ivchat.park.coupons.model.TicketCoupons;
import com.ivchat.park.coupons.model.TicketCouponsIssue;
import com.ivchat.park.coupons.model.TicketCouponsRedundancy;
import com.ivchat.park.coupons.model.TicketMerchant;
import com.ivchat.park.coupons.model.TicketMerchantCoupons;
import com.ivchat.park.coupons.querybean.ParkCouponsManageQueryBean;
import com.ivchat.park.coupons.service.ParkCouponsIssueService;
import com.ivchat.park.coupons.service.ParkCouponsManageService;
import com.ivchat.park.coupons.service.ParkMerchantManageService;
import com.ivchat.park.coupons.service.TicketCouponsRedundancyService;
import com.ivchat.park.coupons.service.TicketMerchantCouponsService;
@Controller("parkCouponsManageAction")
@Scope("prototype")
@ParentPackage("park")
@Namespace("/coupons")
public class ParkCouponsManageAction extends BaseAction{
/**
* id
*/
private static final long serialVersionUID = 1L;
private TicketMerchant ticketMerchant;
private ParkMerchantManageService ticketMerchantService;
private TicketMerchantCouponsService tMerchantCouponsService;
@Resource
private TicketMerchantCouponsService ticketMerchantCouponsService;
private ParkCouponsIssueService parkCouponsIssueService;
private Constant constant;
private String id;
private TicketCoupons ticketCoupons;
private TicketCouponsRedundancy ticketCouponsRedundancy;
private ParkCouponsManageService parkCouponsManageService;
private TicketCouponsRedundancyService ticketCouponsRedundancyService;
private PageBean pageBean;
private boolean relation;
private String s_invalidTime;
/**
* 唯一性校验值/校验ID/校验规则
*/
private String validateValue;
private String customAjaxRule;
private String validateId;
public String execute(){
return SUCCESS;
}
/**
*跳转到添加停车券页面
* @return
*/
public String add(){
return "input";
}
/**
* 前端校验停车券类型的唯一性
* @return
*/
public String uniqueIdentityCard() {
ACLUser user = (ACLUser) getRequest().getSession().getAttribute("user");
if(user==null){
return null;
}
boolean flag = true;
String result = "可以使用";
ParkCouponsManageQueryBean pkcpType = new ParkCouponsManageQueryBean();
pkcpType.setCouponsType(validateValue);
Integer tickInteger = parkCouponsManageService.searchByQueryBean(pkcpType, user);
if(tickInteger > 0) {
flag = false;
result = "停车券类型已存在";
}
ValidateBean vb = new ValidateBean(flag, customAjaxRule, validateId);
AjaxJsonUtil.write(JSONObject.fromObject(vb).toString(), getResponse());
log.info("停车券类型[" + validateValue + "]验证成功,验证结果:" + result);
return null;
}
/**
* 添加停车券类型
* @return
*/
public String save(){
try {
ACLUser user = (ACLUser) getRequest().getSession().getAttribute("user");
if(user==null){
return null;
}
String couponsType = null;
couponsType = ticketCoupons.setCouponsType(ticketCoupons.getCouponsType());
int couponscount = parkCouponsManageService.findCouponsType(couponsType,user);
//判断券名是否存在,存在则不能添加
if(couponscount>0){
AjaxFormResponseUtil.failure(getText("该券名已经存在请重新输入"), getResponse());
}
//判断是否关联,如果关联则将数据添加到商家与券的关联表
if(relation){
TicketMerchantCoupons MerchantCoupons = null;
List<TicketMerchant> list = new ArrayList<TicketMerchant>();
list = ticketMerchantService.findAll(user.getXiaoqu_code());
//如果不存在商家就只保存停车券
if(list.size()==0){
ticketCoupons.setSmallCommunityCode(user.getXiaoqu_code());
ticketCoupons.setCommunityCode(user.getCommunity_code());
ticketCoupons.setState(TicketCoupons.STATE);
ticketCoupons.setTime(new Date());
parkCouponsManageService.save(ticketCoupons);
ticketCouponsRedundancyService.save(ticketCoupons);
AjaxFormResponseUtil.success(getText("添加成功"), getResponse());
return null;
}else{
for(TicketMerchant ticketM:list){
MerchantCoupons = new TicketMerchantCoupons();
MerchantCoupons.setMerchantId(ticketM.getId());
MerchantCoupons.setMerchantName(ticketM.getMerchantName());
MerchantCoupons.setMallbuilderId(ticketM.getMallbuilderId());
MerchantCoupons.setMallbuilderName(ticketM.getMallbuilderName());
MerchantCoupons.setCommunityCode(ticketM.getCommunityCode());
MerchantCoupons.setSmallCommunityCode(ticketM.getSmallCommunityCode());
MerchantCoupons.setUserid(ticketM.getUserid());
MerchantCoupons.setUser(ticketM.getUser());
MerchantCoupons.setTime(new Date());
MerchantCoupons.setCouponsType(ticketCoupons.getCouponsType());
MerchantCoupons.setDeductionFee(ticketCoupons.getDeductionFee());
MerchantCoupons.setMerchantValidityTime(ticketCoupons.getMerchantValidityTime());
MerchantCoupons.setTime(ticketCoupons.getTime());
MerchantCoupons.setUnit(ticketCoupons.getUnit());
MerchantCoupons.setCouponsId(ticketCoupons.getId());
MerchantCoupons.setUserValidityTime(ticketCoupons.getUserValidityTime());
MerchantCoupons.setIsdelete(TicketMerchantCoupons.ISDELETE_LOGIC);
tMerchantCouponsService.save(MerchantCoupons);
}//选择关联商家,同时也要添加券的信息
ticketCoupons.setSmallCommunityCode(user.getXiaoqu_code());
ticketCoupons.setCommunityCode(user.getCommunity_code());
ticketCoupons.setState(TicketCoupons.STATE);
ticketCoupons.setTime(new Date());
parkCouponsManageService.save(ticketCoupons);
ticketCouponsRedundancyService.save(ticketCoupons);
AjaxFormResponseUtil.success(getText("添加成功"), getResponse());
return null;
}
}else{
ticketCoupons.setSmallCommunityCode(user.getXiaoqu_code());
ticketCoupons.setCommunityCode(user.getCommunity_code());
ticketCoupons.setState(TicketCoupons.STATE);
ticketCoupons.setTime(new Date());
parkCouponsManageService.save(ticketCoupons);
ticketCouponsRedundancyService.save(ticketCoupons);
AjaxFormResponseUtil.success(getText("添加成功"), getResponse());
}
} catch (Exception e) {
AjaxFormResponseUtil.failure(getText("添加失败"), getResponse());
}
return null;
}
/**
* 删除:删除停车券的同时,逻辑删除商家和券的关联关系使isdelete更改为0。
* @return
*/
public void delete(){
List<TicketCouponsIssue> list = new ArrayList<TicketCouponsIssue>();
List<TicketCouponsIssue> listEntity = new ArrayList<TicketCouponsIssue>();
try {
if(id!= null){
TicketCoupons ticketCoupons = parkCouponsManageService.get(id);
List<TicketCouponsIssue> unused = parkCouponsIssueService.getUnused(ticketCoupons.getCouponsType());
for(int i=0;i<unused.size();i++){
if(unused.get(i).getUnused()==TicketCouponsIssue.UNUSED){
listEntity.add(unused.get(i));
list.remove(unused.get(i));
}else{
list.add(unused.get(i));
}
}
if(list.size()==0){
parkCouponsManageService.deleteCoupons(ticketCoupons);
//ticketMerchantCouponsService.updateEntity(unused,TicketCouponsIssue.UNUSED);//删除停车券的同时,逻辑删除商家和券的关联关系使isdelete更改为0。
ticketMerchantCouponsService.delete(unused);
parkCouponsIssueService.delete(listEntity);
AjaxFormResponseUtil.success("删除信息成功!", getResponse());
}else{
AjaxFormResponseUtil.failure("优惠券购买的未使用数量为0时才能删除", getResponse());
}
}else {
AjaxFormResponseUtil.failure("请选择要删除的停车券!", getResponse());
}
} catch (Exception e) {
AjaxFormResponseUtil.failure("删除信息失败!", getResponse());
}
}
/**
* 冻结停车券
* @return
* @return
*/
public String frost(){
try {
if(id!= null){
TicketCoupons ticketCoupon = parkCouponsManageService.get(id);
if(ticketCoupon!=null){
parkCouponsManageService.updateFrost(id,TicketCoupons.STATE);
AjaxFormResponseUtil.success("冻结成功!", getResponse());
}else {
AjaxFormResponseUtil.failure("没有该信息请重新选择", getResponse());
}
}else AjaxFormResponseUtil.failure("没有该信息请重新选择!", getResponse());
} catch (Exception e) {
AjaxFormResponseUtil.failure("冻结失败!", getResponse());
}
return null;
}
/**
* 解冻停车券
* @return
* @return
*/
public String freedom(){
String state = null;
try {
if(id!= null){
TicketCoupons ticketCoupon = parkCouponsManageService.get(id);
state = ticketCoupon.getState();
if(state.equals(TicketCoupons.STATE1)){
parkCouponsManageService.updateFreedom(id,TicketCoupons.STATE);
}
AjaxFormResponseUtil.success("解冻成功!", getResponse());
}else{
AjaxFormResponseUtil.failure("没有选中,请重新选择!", getResponse());
}
} catch (Exception e) {
AjaxFormResponseUtil.failure("解冻失败!请重试", getResponse());
}
return null;
}
/**
* 停车券失效
* @return
* @return
*/
public String addlose(){
try {
ticketCoupons = parkCouponsManageService.get(id);
} catch (Exception e) {
e.printStackTrace();
}
return "addlose";
}
/**
* 添加停车券失效时间
* @return
* @return
*/
public String losesave(){
try {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
ticketCoupons =parkCouponsManageService.get(ticketCoupons.getId());
ticketCoupons.setInvalidTime(formatter.parse(s_invalidTime));
parkCouponsManageService.updateLose(ticketCoupons);
AjaxFormResponseUtil.success("添加成功", getResponse());
} catch (Exception e) {
AjaxFormResponseUtil.failure("添加失败", getResponse());
}
return null;
}
/**
* 停车券类型返回页面展示的数据
* @return
*/
public String list(){
ACLUser user = (ACLUser) getRequest().getSession().getAttribute("user");
if(user==null){
return null;
}
DataGridPage data = new DataGridPage();
if(pageBean==null){
pageBean = new PageBean();
}
//思路:先获取到运营商和社区编码和小区的编码,2判断是运营商的可查所有数据,小区的只能看本小区的数据,社区还能看本社区的数据
ParkCouponsManageQueryBean parkCouponsQueryBean = new ParkCouponsManageQueryBean();
if(String.valueOf(Constant.USER_TYPE_SQYY).equals(user.getUsertype())){
data = parkCouponsManageService.getAllByDGP(pageBean,parkCouponsQueryBean);
}else if(String.valueOf(Constant.USER_TYPE_XQ).equals(user.getUsertype())
||String.valueOf(Constant.USER_TYPE_WGJT).equals(user.getUsertype())){
String communityCode = user.getCommunity_code();
String smallCommunityCode = user.getXiaoqu_code();
parkCouponsQueryBean.setCommunityCode(communityCode);
parkCouponsQueryBean.setSmallCommunityCode(smallCommunityCode);
data = parkCouponsManageService.getAllByDGP(pageBean,parkCouponsQueryBean);
}
FlexGridExtUtil.JsonDataToWeb(data, getResponse(),"yyyy-MM-dd");
return null;
}
public boolean isRelation() {
return relation;
}
public void setRelation(boolean relation) {
this.relation = relation;
}
public TicketCoupons getTicketCoupons() {
return ticketCoupons;
}
public void setTicketCoupons(TicketCoupons ticketCoupons) {
this.ticketCoupons = ticketCoupons;
}
public ParkCouponsManageService getParkCouponsManageService() {
return parkCouponsManageService;
}
public void setParkCouponsManageService(
ParkCouponsManageService parkCouponsManageService) {
this.parkCouponsManageService = parkCouponsManageService;
}
public PageBean getPageBean() {
return pageBean;
}
public void setPageBean(PageBean pageBean) {
this.pageBean = pageBean;
}
public static String getState() {
return TicketCoupons.STATE;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getS_invalidTime() {
return s_invalidTime;
}
public void setS_invalidTime(String sInvalidTime) {
s_invalidTime = sInvalidTime;
}
public String getCustomAjaxRule() {
return customAjaxRule;
}
public void setCustomAjaxRule(String customAjaxRule) {
this.customAjaxRule = customAjaxRule;
}
public String getValidateId() {
return validateId;
}
public void setValidateId(String validateId) {
this.validateId = validateId;
}
public Constant getConstant() {
return constant;
}
public void setConstant(Constant constant) {
this.constant = constant;
}
public String getValidateValue() {
return validateValue;
}
public void setValidateValue(String validateValue) {
this.validateValue = validateValue;
}
public ParkMerchantManageService getTicketMerchantService() {
return ticketMerchantService;
}
public void setTicketMerchantService(ParkMerchantManageService ticketMerchantService) {
this.ticketMerchantService = ticketMerchantService;
}
public TicketMerchant getTicketMerchant() {
return ticketMerchant;
}
public void setTicketMerchant(TicketMerchant ticketMerchant) {
this.ticketMerchant = ticketMerchant;
}
public TicketMerchantCouponsService getTicketMerchantCouponsService() {
return ticketMerchantCouponsService;
}
public void setTicketMerchantCouponsService(
TicketMerchantCouponsService ticketMerchantCouponsService) {
this.ticketMerchantCouponsService = ticketMerchantCouponsService;
}
public ParkCouponsIssueService getParkCouponsIssueService() {
return parkCouponsIssueService;
}
public void setParkCouponsIssueService(
ParkCouponsIssueService parkCouponsIssueService) {
this.parkCouponsIssueService = parkCouponsIssueService;
}
public TicketMerchantCouponsService gettMerchantCouponsService() {
return tMerchantCouponsService;
}
public void settMerchantCouponsService(
TicketMerchantCouponsService tMerchantCouponsService) {
this.tMerchantCouponsService = tMerchantCouponsService;
}
public TicketCouponsRedundancyService getTicketCouponsRedundancyService() {
return ticketCouponsRedundancyService;
}
public void setTicketCouponsRedundancyService(
TicketCouponsRedundancyService ticketCouponsRedundancyService) {
this.ticketCouponsRedundancyService = ticketCouponsRedundancyService;
}
public TicketCouponsRedundancy getTicketCouponsRedundancy() {
return ticketCouponsRedundancy;
}
public void setTicketCouponsRedundancy(
TicketCouponsRedundancy ticketCouponsRedundancy) {
this.ticketCouponsRedundancy = ticketCouponsRedundancy;
}
}