package com.huiju.estateagents.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.common.CommConstant;
import com.huiju.estateagents.entity.*;
import com.huiju.estateagents.excel.HelpInitiateRecordSucceed;
import com.huiju.estateagents.excel.HelpProceedAndFinish;
import com.huiju.estateagents.excel.HelpRecordExport;
import com.huiju.estateagents.excel.PublicCustomerExport;
import com.huiju.estateagents.excel.handler.CustomCellWriteHandler;
import com.huiju.estateagents.mapper.HelpInitiateRecordMapper;
import com.huiju.estateagents.reportCustomerAop.ReportCustomer;
import com.huiju.estateagents.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
    * 助力活动  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-10-17
 */
@RestController
@RequestMapping("/api")
@Api(value = "活动助力", tags = "活动助力")
public class HelpActivityController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(HelpActivityController.class);

    @Autowired
    public IHelpActivityService taHelpActivityService;

    @Autowired
    private ITaBuildingService iTaBuildingService;
    
    @Autowired
    private ITaPersonService taPersonService;
    
    @Autowired
    private IHelpInitiateRecordService helpInitiateRecordService;
    
    @Autowired
    private IHelpRecordService helpRecordService;
    
    @Autowired
    private TaPosterService taPosterService;
    
    @Autowired
    private TaShareContentService taShareContentService;

    @Autowired
    private HelpInitiateRecordMapper helpInitiateRecordMapper;
    
    @Autowired
    private ITdPosterTemplateService tdPosterTemplateService;

    @Autowired
    private IExtendContentService iExtendContentService;
    /**
     * 活动助力列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "活动助力列表", notes = "活动助力列表")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "pageNum", paramType = "query", value = "第几页"),
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "pageSize", paramType = "query", value = "一页多少行"),
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "cityId", paramType = "query", value = "城市"),
            @ApiImplicitParam(dataTypeClass = String.class, name = "buildingId", paramType = "query", value = "项目"),
            @ApiImplicitParam(dataTypeClass = String.class, name = "title", paramType = "query", value = "标题"),
            @ApiImplicitParam(dataTypeClass = String.class, name = "startDate", paramType = "query", value = "活动开始时间"),
            @ApiImplicitParam(dataTypeClass = String.class, name = "endDate", paramType = "query", value = "活动结束时间"),
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "activityStatus", paramType = "query", value = "活动状态")
    })
    @RequestMapping(value = "/admin/helpActivity/list", method = RequestMethod.GET)
    public ResponseBean helpActivityList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "cityId", required = false) Integer cityId,
                                         @RequestParam(value = "buildingId", required = false) String buildingId,
                                         @RequestParam(value = "title", required = false) String title,
                                         @RequestParam(value = "startDate", required = false) String startDate,
                                         @RequestParam(value = "endDate", required = false) String endDate,
                                         @RequestParam(value = "activityStatus", required = false) Integer activityStatus,
                                         HttpServletRequest request) {
        Integer orgId = getOrgId(request);
        ResponseBean helpActivityList = taHelpActivityService.helpActivityList(pageNum, pageSize, cityId, buildingId, title, startDate, endDate, activityStatus, orgId,getTaPersonBuildingListByUserId(request));
        return helpActivityList;
    }

    /**
     * 活动助力列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "活动助力列表 已开始 和 未开始", notes = "活动助力列表 已开始 和 未开始")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "pageNum", paramType = "query", value = "第几页"),
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "pageSize", paramType = "query", value = "一页多少行"),
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "cityId", paramType = "query", value = "城市"),
    })
    @RequestMapping(value = "/admin/helpActivityEffective/list", method = RequestMethod.GET)
    public ResponseBean helpActivityEffectiveList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "cityId", required = false) Integer cityId,
                                         @RequestParam(value = "buildingId", required = false) String buildingId,
                                         @RequestParam(value = "status", required = false) Integer status,
                                         @RequestParam(value = "name", required = false) String name,
                                         HttpServletRequest request) {
        Integer orgId = getOrgId(request);
        ResponseBean helpActivityList = taHelpActivityService.helpActivityEffectiveList(pageNum, pageSize, cityId, orgId, buildingId,status,name,getTaPersonBuildingListByUserId(request));
        return helpActivityList;
    }

    @ApiOperation(value = "活动助力新增", notes = "活动助力新增")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "HelpActivity", name = "parameter", paramType = "body", value = "当前对象")
    })
    @RequestMapping(value = "/admin/helpActivity/add", method = RequestMethod.POST)
    public ResponseBean helpActivityAdd(@RequestBody String parameter, HttpServletRequest request) {
        HelpActivity helpActivity = JSONObject.parseObject(parameter, HelpActivity.class);
        ResponseBean responseBean = new ResponseBean();
        Integer orgId = getOrgId(request);
        Integer cityId = iTaBuildingService.getCityById(helpActivity.getBuildingId());
        QueryWrapper<HelpActivity> helpActivityQueryWrapper = new QueryWrapper<>();
        helpActivityQueryWrapper.eq("heavy", helpActivity.getHeavy());
//        Integer heavyCount = taHelpActivityService.count(helpActivityQueryWrapper);
//        if (heavyCount != 0){
//            responseBean.addError("权重已存在，请重新编辑");
//            return responseBean;
//        }
        helpActivity.setCityId(cityId);
        // 活动暂且定位为未开始
        helpActivity.setActivityStatus(1);
        helpActivity.setStatus(1);
        helpActivity.setWeight(0);
        helpActivity.setIsMain(false);
         int id = taHelpActivityService.helpActivityAdd(helpActivity, orgId);
        responseBean.addSuccess(id);
        return responseBean;
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     */
    @ResponseBody
    @RequestMapping(value = "/helpActivity/{id}", method = RequestMethod.DELETE)
    public ResponseBean helpActivityDelete(@PathVariable Integer id) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (taHelpActivityService.removeById(id)) {
                responseBean.addSuccess("success");
            } else {
                responseBean.addError("fail");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("helpActivityDelete -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }


    @ApiOperation(value = "活动助力修改", notes = "活动助力修改")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = HelpActivity.class, name = "helpActivity", paramType = "body", value = "当前对象"),
    })
    @RequestMapping(value = "admin/helpActivity/update", method = RequestMethod.PUT)
    public ResponseBean helpActivityUpdate(@RequestBody String parameter) {
        ResponseBean responseBean = new ResponseBean();
        HelpActivity helpActivity = JSONObject.parseObject(parameter, HelpActivity.class);
        QueryWrapper<HelpActivity> helpActivityQueryWrapper = new QueryWrapper<>();
        helpActivityQueryWrapper.eq("heavy", helpActivity.getHeavy());
        helpActivityQueryWrapper.ne("help_activity_id", helpActivity.getHelpActivityId());
//        Integer heavyCount = taHelpActivityService.count(helpActivityQueryWrapper);
//        if (heavyCount != 0){
//            responseBean.addError("权重已存在，请重新编辑");
//            return responseBean;
//        }
        Integer cityId = iTaBuildingService.getCityById( helpActivity.getBuildingId());
        helpActivity.setCityId(cityId);
        helpActivity.setHelpActivityId(helpActivity.getHelpActivityId());
        taHelpActivityService.helpActivityUpdate(helpActivity);
        return responseBean;
    }

    @ApiOperation(value = "根据id查询活动助力详情", notes = "根据id查询活动助力详情")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "helpActivityId", paramType = "query", value = "活动状态")
    })
    @RequestMapping(value = "admin/helpActivity/details", method = RequestMethod.GET)
    public ResponseBean helpActivityGet(@RequestParam(value = "helpActivityId", required = false) Integer helpActivityId) {
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(taHelpActivityService.getById(helpActivityId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("helpActivityDelete -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    @ApiOperation(value = "置顶和推首页", notes = "置顶和推首页")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class, name = "parameter", paramType = "body", value = "status:0置顶操作，1推首页操作,2结束活动操作,helpActivityId:助力ID")
    })
    @RequestMapping(value = "admin/top/update", method = RequestMethod.POST)
    public ResponseBean top(@RequestBody String parameter) {
        ResponseBean responseBean = new ResponseBean();
        JSONObject object = JSONObject.parseObject(parameter);
        // 根据状态值判断是置顶还是推首页的操作
        String activityStatus = object.getString("status");
        String helpActivityId = object.getString("helpActivityId");
        HelpActivity helpActivity = taHelpActivityService.getById(helpActivityId);
        // 置顶操作
        if (activityStatus.equals("0")) {
            helpActivity.setWeight(Integer.valueOf(helpActivity.getWeight().equals(0) ? 1 : 0));
        }
        // 推首页
        if (activityStatus.equals("1")) {
            helpActivity.setIsMain(true == helpActivity.getIsMain() ? false : true);
        }
        // 结束活动操作
        if (activityStatus.equals("2")) {
            helpActivity.setActivityStatus(2);
            QueryWrapper<HelpInitiateRecord> helpInitiateRecordQuery= new QueryWrapper<>();
            helpInitiateRecordQuery.eq("help_activity_id",helpActivityId);
                List<HelpInitiateRecord> list= helpInitiateRecordMapper.selectList(helpInitiateRecordQuery);
                for (HelpInitiateRecord helpInitiateRecord:list){
                    // 助力成功的不更新。
                    if (!helpInitiateRecord.getStatus().equals(0)) {
                        helpInitiateRecord.setStatus(2);
                        helpInitiateRecordMapper.updateById(helpInitiateRecord);
                    }
                }
        }
        Boolean isA = taHelpActivityService.updateById(helpActivity);
        responseBean.addSuccess(isA);
        return responseBean;
    }

    @RequestMapping(value = "/admin/helpActivity/send/{helpActivityId}", method = RequestMethod.PUT)
    public ResponseBean send(@PathVariable String helpActivityId, HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        QueryWrapper<HelpActivity> helpActivityQueryWrapper = new QueryWrapper<>();
        helpActivityQueryWrapper.eq("help_activity_id", helpActivityId);
        HelpActivity helpActivity = taHelpActivityService.getOne(helpActivityQueryWrapper);
        helpActivity.setStatus(1);
        helpActivity.setOrgId(getOrgId(request));
        taHelpActivityService.update(helpActivity, helpActivityQueryWrapper);
        return responseBean;
    }

    @RequestMapping(value = "/admin/helpActivity/cancel/{helpActivityId}", method = RequestMethod.PUT)
    public ResponseBean cancel(@PathVariable String helpActivityId, HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        QueryWrapper<HelpActivity> taShareActivityQueryWrapper = new QueryWrapper<>();
        taShareActivityQueryWrapper.eq("help_activity_id", helpActivityId);
        HelpActivity helpActivity = taHelpActivityService.getOne(taShareActivityQueryWrapper);
        helpActivity.setStatus(-1);
        helpActivity.setOrgId(getOrgId(request));
        taHelpActivityService.update(helpActivity, taShareActivityQueryWrapper);
        // 设置为取消发布以后把轮播图和资讯设置为停用
        iExtendContentService.updateActivity(helpActivityId,"help",getOrgId(request));
        return responseBean;
    }

    @ApiOperation(value = "助力记录", notes = "助力记录")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "pageNum", paramType = "query", value = "第几页"),
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "pageSize", paramType = "query", value = "一页多少行"),
            @ApiImplicitParam(dataTypeClass = String.class, name = "helpActivityId", paramType = "query", value = "助力ID"),
            @ApiImplicitParam(dataTypeClass = String.class, name = "tel", paramType = "query", value = "tel"),
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "status", paramType = "query", value = "status: helpSucceed助力成功，helpUnderway进行中，helpUnfinished助力失败")
    })
    @RequestMapping(value = "admin/helpRecord", method = RequestMethod.GET)
    public ResponseBean helpRecord(@RequestParam(value = "helpActivityId", required = false) Integer helpActivityId,
                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                   @RequestParam(value = "status", required = false) String status,
                                   @RequestParam(value = "tel", required = false) String tel) {
        ResponseBean helpRecord = taHelpActivityService.helpRecord(helpActivityId, status, pageNum, pageSize,tel);
        return helpRecord;
    }

    @ApiOperation(value = "助力人详情", notes = "助力人详情")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "pageNum", paramType = "query", value = "第几页"),
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "pageSize", paramType = "query", value = "一页多少行"),
            @ApiImplicitParam(dataTypeClass = String.class, name = "helpRecordInitiateId", paramType = "query", value = "发起助力ID"),
    })
    @RequestMapping(value = "admin/taHelpRecord/details", method = RequestMethod.GET)
    public ResponseBean taHelpRecordDetails(@RequestParam(value = "helpRecordInitiateId", required = false) Integer helpRecordInitiateId,
                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        ResponseBean helpRecord = taHelpActivityService.taHelpRecordDetails(helpRecordInitiateId, pageNum, pageSize);
        return helpRecord;
    }

    @ApiOperation(value = "核销", notes = "核销")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "pageNum", paramType = "query", value = "第几页"),
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "pageSize", paramType = "query", value = "一页多少行"),
            @ApiImplicitParam(dataTypeClass = String.class, name = "helpRecordInitiateId", paramType = "query", value = "发起助力ID"),
            @ApiImplicitParam(dataTypeClass = String.class, name = "verifyCode", paramType = "query", value = "核销码"),
    })
    @RequestMapping(value = "admin/helpInitiateRecordVerify", method = RequestMethod.POST)
    public ResponseBean helpInitiateRecordVerify(@RequestParam(value = "helpRecordInitiateId", required = false) Integer helpRecordInitiateId,
                                                 @RequestParam(value = "verifyCode", required = false) String verifyCode) {
        ResponseBean helpRecord = taHelpActivityService.helpInitiateRecordVerify(helpRecordInitiateId, verifyCode);
        return helpRecord;
    }

    /**
     * 导出数据 成功/进行/失败
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @GetMapping("/admin/HelpInitiateRecordSucceed/export")
    public void getCustomerExportList(HttpServletRequest request,
                                      HttpServletResponse response,
                                      @RequestParam(value = "tel", required = false) String tel,
                                      @RequestParam(value = "helpActivityId", required = false) Integer helpActivityId,
                                      @RequestParam(value = "condition", required = false) String condition) throws IOException {
        //        response.setContentType("application/vnd.ms-excel");
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=助力活动发起人.xlsx");

        Integer orgId = getOrgId(request);

        Integer pageCode = 0;
        Integer pageSize = 100;
        // 总页数
        Integer pageCount = 0;

        /**
         * 助力活动发起者
         */
        // 设置读取的class
       // code：helpSucceed助力成功，helpUnderway进行中，helpUnfinished助力失败
        if ("helpSucceed".equals(condition)){
            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), HelpInitiateRecordSucceed.class).registerWriteHandler(new CustomCellWriteHandler()).build();
            // 设置 sheet, 同一个sheet只需要设置一次
            WriteSheet writeSheet = EasyExcel.writerSheet("助力成功").build();
            Integer count = taHelpActivityService.getHelpInitiateRecordSucceed(tel,helpActivityId);
            pageCount = count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1;
            List<HelpInitiateRecordSucceed> publicList = null;
            do {
                publicList = taHelpActivityService.getHelpInitiateRecordSucceedExportList(pageCode * pageSize, pageSize,tel,helpActivityId);
                excelWriter.write(publicList, writeSheet);

                pageCode++;
            } while (pageCode <= pageCount);

            // finish 会帮忙关闭流
            excelWriter.finish();
        }else {
            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), HelpProceedAndFinish.class).registerWriteHandler(new CustomCellWriteHandler()).build();
            // 设置 sheet, 同一个sheet只需要设置一次
            WriteSheet writeSheet = EasyExcel.writerSheet("助力成功").build();
            Integer count = taHelpActivityService.getHelpInitiateRecordSucceed(tel,helpActivityId);
            pageCount = count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1;
            List<HelpProceedAndFinish> publicList = null;
            do {
                publicList = taHelpActivityService.getHelpProceedAndFinishExportList(pageCode * pageSize, pageSize,tel,helpActivityId,condition);
                excelWriter.write(publicList, writeSheet);

                pageCode++;
            } while (pageCode <= pageCount);

            // finish 会帮忙关闭流
            excelWriter.finish();
        }

    }

    /**
     * 助力活动查询当前助力者记录
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @GetMapping("/admin/helpRecord/export")
    public void gethelpRecordExport(HttpServletRequest request,
                                      HttpServletResponse response,
                                      @RequestParam(value = "helpRecordInitiateId", required = false) Integer helpRecordInitiateId) throws IOException {
        //        response.setContentType("application/vnd.ms-excel");
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=助力活动发起人.xlsx");

        Integer orgId = getOrgId(request);

        Integer pageCode = 0;
        Integer pageSize = 100;
        // 总页数
        Integer pageCount = 0;

        /**
         * 助力活动发起者
         */
        // 设置读取的class
            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), HelpRecordExport.class).registerWriteHandler(new CustomCellWriteHandler()).build();
            // 设置 sheet, 同一个sheet只需要设置一次
            WriteSheet writeSheet = EasyExcel.writerSheet("助力成功").build();
            Integer count = taHelpActivityService.getHelpRecord(helpRecordInitiateId);
            pageCount = count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1;
            List<HelpRecordExport> publicList = null;
            do {
                publicList = taHelpActivityService.getHelpRecordExportList(pageCode * pageSize, pageSize,helpRecordInitiateId);
                excelWriter.write(publicList, writeSheet);

                pageCode++;
            } while (pageCode <= pageCount);

            // finish 会帮忙关闭流
            excelWriter.finish();
    }
    
    @ApiOperation(value = "微信端根据id查询活动助力详情", notes = "根据id查询活动助力详情")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "helpActivityId", paramType = "path", value = "活动状态")
    })
    @RequestMapping(value = "/wx/helpActivity/{helpActivityId}", method = RequestMethod.GET)
    @ReportCustomer(type = "help")
    public ResponseBean wxHelpActivityGet(@PathVariable(value = "helpActivityId") Integer helpActivityId,
                                          @RequestParam(value = "qrCodeId", required = false) String qrCodeId,
                                          @RequestParam(value = "channelId", required = false) String channelId,
                                          @RequestParam(value = "sceneId", required = false) String sceneId,
                                          HttpServletRequest request, @RequestParam(value = "initiateId",required = false) Integer initiateId) {
        ResponseBean responseBean = new ResponseBean();
        Integer orgId = getOrgId(request);
        String openid = getOpenId(request);
        List<TaPerson> taPersons = taPersonService.getPersonsByOpenId(openid);
        if (null == taPersons || taPersons.size() != 1) {
            return ResponseBean.error("验证人员信息失败", ResponseBean.ERROR_UNAVAILABLE);
        }
        TaPerson person = taPersons.get(0);
        Map<String,Object> map = new HashMap<>();
        try {
            //活动信息
            HelpActivity helpActivity = taHelpActivityService.getById(helpActivityId);
            QueryWrapper<HelpInitiateRecord> helpInitiateRecordQueryWrapper = new QueryWrapper<>();
            helpInitiateRecordQueryWrapper.eq("org_id",orgId);
            helpInitiateRecordQueryWrapper.eq("help_activity_id",helpActivity.getHelpActivityId());
            helpInitiateRecordQueryWrapper.eq("status",CommConstant.STATUS_UNACCALIMED);
            //已经成功的助力
            List<HelpInitiateRecord> helpInitiateRecordList = helpInitiateRecordMapper.selectList(helpInitiateRecordQueryWrapper);
            helpActivity.setSuccessNum(helpInitiateRecordList.size());
            map.put("helpActivity",helpActivity);
            
            //发起者信息
            HelpInitiateRecord helpInitiateRecord = null;
             if (null != initiateId){
                 helpInitiateRecord = helpInitiateRecordService.getById(initiateId);
             }else {
                 QueryWrapper<HelpInitiateRecord> initiateRecordQueryWrapper = new QueryWrapper<>();
                 initiateRecordQueryWrapper.eq("org_id",orgId);
                 initiateRecordQueryWrapper.eq("help_activity_id",helpActivity.getHelpActivityId());
                 initiateRecordQueryWrapper.eq("person_id",person.getPersonId());
                 helpInitiateRecord = helpInitiateRecordService.getOne(initiateRecordQueryWrapper);
             }
             
             //判断是否发起者
            if (null != helpInitiateRecord){
                helpActivity.setHelpStatus(CommConstant.HELP_STATUS_INITIATE);
        
                //助力者信息
                QueryWrapper<HelpRecord> helpRecordQueryWrapper = new QueryWrapper<>();
                helpRecordQueryWrapper.eq("org_id",orgId);
                helpRecordQueryWrapper.eq("help_activity_id",helpActivity.getHelpActivityId());
                helpRecordQueryWrapper.eq("help_record_initiate_id",helpInitiateRecord.getHelpRecordInitiateId());
                List<HelpRecord> helpRecordList = helpRecordService.list(helpRecordQueryWrapper);
                helpRecordList.forEach(e -> {
                    if (e.getPersonId().equals(person.getPersonId())){
                        map.put("isJoin",true);
                    }
                });
        
                map.put("helpInitiateRecord",helpInitiateRecord);
                map.put("helpRecordList",helpRecordList);
            }
    
            //获取海报信息
            QueryWrapper<TaPoster> posterQueryWrapper = new QueryWrapper<>();
            posterQueryWrapper.eq("target_type",CommConstant.HELP_ACTIVITY);
            posterQueryWrapper.eq("target_id",helpActivityId);
            posterQueryWrapper.eq("org_id",orgId);
            List<TaPoster> postList = taPosterService.list(posterQueryWrapper);
            map.put("postList",postList);
    
            QueryWrapper<TdPosterTemplate> tdPosterTemplateQueryWrapper = new QueryWrapper<>();
            //tdPosterTemplateQueryWrapper.eq("org_id",orgId);
            tdPosterTemplateQueryWrapper.eq("status",CommConstant.STATUS_NORMAL);
            tdPosterTemplateQueryWrapper.eq("type",CommConstant.HELP_ACTIVITY);
            List<TdPosterTemplate> posterTemplateList = tdPosterTemplateService.list(tdPosterTemplateQueryWrapper);
            map.put("posterTemplateList",posterTemplateList);
            
            //获取分享信息
            QueryWrapper<TaShareContent> taShareContentQueryWrapper = new QueryWrapper<>();
            taShareContentQueryWrapper.eq("share_content_type",CommConstant.HELP_ACTIVITY);
            taShareContentQueryWrapper.eq("target_id",helpActivityId);
            taShareContentQueryWrapper.eq("org_id",orgId);
            List<TaShareContent> shareContentList = taShareContentService.list(taShareContentQueryWrapper);
            map.put("shareContentList",shareContentList);
    
            responseBean.addSuccess(map);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("helpActivityDelete -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
    @ApiOperation(value = "微信端查询拼团和助力推首页的活动的", notes = "微信端查询拼团和助力推首页的活动的")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "buildingId", paramType = "query", value = "活动状态"),
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "type", paramType = "query", value = "1助力，2拼团"),
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "type", paramType = "query", value = "cityId首页专用")
    })
    @RequestMapping(value = "/wx/helpActivityAndGroup", method = RequestMethod.GET)
    public ResponseBean wxhelpActivityAndGroup(HttpServletRequest request,
                                               @RequestParam(value = "buildingId",required = false) String buildingId,
                                               @RequestParam(value = "cityId",required = false) String cityId) {
            String openid = getOpenId(request);
            ResponseBean  responseBean = new ResponseBean();
            List<TaPerson> taPersons = taPersonService.getPersonsByOpenId(openid);
            if (null == taPersons || taPersons.size() != 1) {
                return responseBean.error("验证人员信息失败", ResponseBean.ERROR_UNAVAILABLE);
            }
            TaPerson person = taPersons.get(0);
            ResponseBean lsit = taHelpActivityService.wxhelpActivityAndGroup(getOrgId(request),buildingId,cityId, person);
            return lsit;
        }

}