package com.sqx.modules.resumes.service.impl;


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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.entity.UserBrowse;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.entity.UserMoneyDetails;
import com.sqx.modules.app.service.UserBrowseService;
import com.sqx.modules.app.service.UserMoneyDetailsService;
import com.sqx.modules.app.service.UserMoneyService;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.firstLogin.service.HrService;
import com.sqx.modules.myCollection.entity.MyCollection;
import com.sqx.modules.myCollection.service.MyCollectionService;
import com.sqx.modules.pay.entity.PayDetails;
import com.sqx.modules.pay.service.PayDetailsService;
import com.sqx.modules.resumes.dao.PostPushDao;
import com.sqx.modules.resumes.dao.RuleClassifyDao;
import com.sqx.modules.resumes.entity.Company;
import com.sqx.modules.resumes.entity.CompanyUser;
import com.sqx.modules.resumes.entity.PostPush;
import com.sqx.modules.resumes.entity.RuleClassify;
import com.sqx.modules.resumes.service.CompanyService;
import com.sqx.modules.resumes.service.CompanyUserService;
import com.sqx.modules.resumes.service.PostPushService;
import com.sqx.modules.resumes.vo.CityVo;
import com.sqx.modules.resumes.vo.LetterCity;
import com.sqx.modules.utils.SenInfoCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 岗位推送
 */
@Slf4j
@Service
public class PostPushServiceImpl extends ServiceImpl<PostPushDao, PostPush> implements PostPushService {
    @Autowired
    private UserBrowseService browseService;

    @Autowired
    private UserBrowseService userBrowseService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserMoneyService userMoneyService;
    @Autowired
    private UserMoneyDetailsService userMoneyDetailsService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private CommonInfoService commonInfoService;
    @Autowired
    private MyCollectionService myCollectionService;
    @Autowired
    private RuleClassifyDao ruleClassifyDao;
    private ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
    @Autowired
    private HrService hrService;

//    @Override
//    public Result savePostPush(PostPush postPush, Long userId) {
//        if (userId == null) {
//            userId = companyService.getById(postPush.getCompanyId()).getUserId();
//        }
//        QueryWrapper<PostPush> wrapper = new QueryWrapper<PostPush>().eq("rule_classify_id", postPush.getRuleClassifyId()).eq("company_id", postPush.getCompanyId()).notIn("status", 3, 4);
//        reentrantReadWriteLock.writeLock().lock();
//        try {
//            if (postPush.getPostPushId() != null) {
//                PostPush push = baseMapper.selectById(postPush.getPostPushId());
//                if (push.getStatus() != 2 && push.getStatus() != 3 && push.getStatus() != 5) {
//                    return Result.error("当前状态不能修改,请先取消");
//                }
//                if (push.getStatus() == 5) {
//                    if (LocalDateTime.now().isAfter(push.getOverdueTime())) {
//                        return Result.error("当前招聘信息已过期");
//                    }
//                }
//                //过滤当前数据,否则会因相同岗位限制而无法修改
//                wrapper.ne("post_push_id", postPush.getPostPushId());
//            }
//            Integer count = baseMapper.selectCount(wrapper);
//            if (count > 0) {
//                return Result.error("相同岗位只能发布一条招聘信息");
//            }
//            RuleClassify ruleClassify = ruleClassifyDao.selectById(postPush.getRuleClassifyId());
//            postPush.setStatus(1);
//            postPush.setRuleClassifyName(ruleClassify.getRuleClassifyName());
//            postPush.setUpdateTime(DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
//            UserEntity userEntity1 = userService.selectUserById(userId);
//            if(userEntity1.getIsCompanyVip()!=null && userEntity1.getIsCompanyVip()==1){
//                postPush.setPayMoney(ruleClassify.getMemberPrice());
//            }else{
//                postPush.setPayMoney(ruleClassify.getPrice());
//            }
//            postPush.setCityInitial(PinYinUti.getFirstPinYin(postPush.getCity()));
//            RuleClassify twoClassify = ruleClassifyDao.selectOne(new QueryWrapper<RuleClassify>().eq("rule_classify_id", ruleClassify.getParentId()));
//            RuleClassify oneClassify = ruleClassifyDao.selectOne(new QueryWrapper<RuleClassify>().eq("rule_classify_id", twoClassify.getParentId()));
//            postPush.setClassifyTwoId(twoClassify.getRuleClassifyId());
//            postPush.setClassifyOneId(oneClassify.getRuleClassifyId());
//            UserMoney userMoney = userMoneyService.selectUserMoneyByUserId(userId);
//            //当支付金额大于0时才支付。
//            if (postPush.getPayMoney().compareTo(BigDecimal.ZERO) > 0) {
//                if(userEntity1.getIsCompanyVip()!=null && userEntity1.getIsCompanyVip()==1){
//                    if (ruleClassify.getMemberPrice().compareTo(userMoney.getMoney()) > 0) {
//                        return Result.error("账户余额不足，请充值！");
//                    }
//                }else{
//                    if (ruleClassify.getPrice().compareTo(userMoney.getMoney()) > 0) {
//                        return Result.error("账户余额不足，请充值！");
//                    }
//                }
//                userMoneyService.updateMoney(2, userId, postPush.getPayMoney());
//                UserMoneyDetails userMoneyDetails = new UserMoneyDetails();
//                userMoneyDetails.setMoney(postPush.getPayMoney());
//                userMoneyDetails.setUserId(userId);
//                userMoneyDetails.setTitle("发布招募岗位成功");
//                userMoneyDetails.setContent("扣除账户钻石：" + postPush.getPayMoney());
//                userMoneyDetails.setType(2);
//                userMoneyDetails.setClassify(1);
//                userMoneyDetails.setCreateTime(DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
//                userMoneyDetailsService.save(userMoneyDetails);
//                Company company = companyService.getById(postPush.getCompanyId());
//                UserEntity userEntity = userService.getById(company.getUserId());
//                if (StringUtils.isNotBlank(userEntity.getOpenId())) {
//                    sendPostMeg(userEntity, postPush);
//                }
//            }
//            //是否是修改操作
//            if (postPush.getPostPushId() != null) {
//                return Result.upStatus(baseMapper.updateById(postPush));
//            } else {
//                postPush.setCreateTime(LocalDateTime.now());
//                return Result.upStatus(baseMapper.insert(postPush));
//            }
//
//        } catch (Exception e) {
//            log.error("岗位发布异常：" + e.getMessage(), e);
//        } finally {
//            reentrantReadWriteLock.writeLock().unlock();
//        }
//        return Result.error("系统繁忙，请稍后再试！");
//    }
    @Autowired
    private CompanyUserService companyUserService;
    @Autowired
    private PayDetailsService payDetailsService;
    @Autowired
    private PostPushService postPushService;

    @Override
    public Result savePostPush(PostPush postPush, Long userId) {
        postPush.setUpdateTime(String.valueOf(LocalDateTime.now()));
        QueryWrapper<CompanyUser> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).eq("is_use",1);
        CompanyUser companyUser = companyUserService.getOne(queryWrapper);
        if (companyUser == null) {//初次注册
            postPush.setStatus(1);
            postPush.setCreateTime(LocalDateTime.now());
            postPush.setUpdateTime(String.valueOf(LocalDateTime.now()));
            postPushService.saveOrUpdate(postPush);
            return Result.success("招聘信息已暂存，等待公司认证后再次发布").put("data",postPush.getPostPushId());
        } else {
            Company company = companyService.getById(companyUser.getCompanyId());
            if (company.getStatus() != 2) {
                return Result.error("公司信息不完整或未通过审核").put("data",postPush.getPostPushId());
            } else {//公司存在判断是更改还是发布新的
                if (postPush.getPostPushId() != null) {//更改
                    PostPush push = baseMapper.selectById(postPush.getPostPushId());
//                    if (push.getStatus() != 2 && push.getStatus() != 3 && push.getStatus() != 5) {
//                        return Result.error("当前状态不能修改,请先取消").put("data",postPush.getPostPushId());
//                    }
//                    if (push.getStatus() == 5) {
//                        return Result.error("当前招聘信息已过期").put("data",postPush.getPostPushId());
//                    } else {
                        postPush.setCompanyId(companyUser.getCompanyId());
                        postPush.setStatus(1);
                        postPush.setUpdateTime(String.valueOf(LocalDateTime.now()));
                        baseMapper.updateById(postPush);
                        return Result.success("更新岗位信息中，请等待审核").put("data",postPush.getPostPushId());

                } else {//新发布,发布岗位相同,不能发布
                    UserEntity userEntity = userService.getById(userId);
                    postPush.setUserId(String.valueOf(userId));
                    postPush.setCompanyId(company.getCompanyId());
                    postPush.setCreateTime(LocalDateTime.now());
                    postPush.setUpdateTime(String.valueOf(LocalDateTime.now()));
                    postPush.setStatus(1);
                    QueryWrapper<PostPush> queryWrapperClassify = new QueryWrapper<>();
                    queryWrapperClassify.eq("rule_classify_id", postPush.getRuleClassifyId()).eq("user_id", userId).notIn("status", 3, 4);
                    PayDetails payDetails = payDetailsService.getOne(new QueryWrapper<PayDetails>().eq("order_id", postPush.getOrderId()));
                    Integer count = baseMapper.selectCount(queryWrapperClassify);
                    if (count > 0) {
                        return Result.error("相同岗位只能发布一条招聘信息").put("data",postPush.getPostPushId());
                    } else {//发布岗位不同
                        if (userEntity.getIsUserVip() == 0) {//发布者是不是vip
                            if (payDetails == null || payDetails.getState() != 1) {
                                return Result.error("请完成订单支付").put("data",postPush.getPostPushId());
                            } else {
                                postPushService.saveOrUpdate(postPush);
                                return Result.success("新发布的岗位正在审核中请等待").put("data",postPush.getPostPushId());
                            }
                        } else {//发布者是vip
                            if (userEntity.getVipPostTimes() == 0) {//没有免费次数
                                if (payDetails == null || payDetails.getState() != 1) {
                                    return Result.error("请完成订单支付").put("data",postPush.getPostPushId());
                                } else {
                                    postPushService.saveOrUpdate(postPush);
                                    return Result.success("新发布的岗位正在审核中请等待").put("data",postPush.getPostPushId());
                                }
                            } else {//有免费次数
                                userEntity.setVipPostTimes(userEntity.getVipPostTimes() - 1);
                                userService.updateById(userEntity);
                                postPushService.saveOrUpdate(postPush);
                                return Result.success("新发布的岗位正在审核中请等待").put("data",postPush.getPostPushId());
                            }
                        }
                    }
                }
            }
        }
    }
    public Result addDue(Long postPushId,Long orderId,Long userId) {
        PostPush postPush = baseMapper.selectById(postPushId);
        UserEntity userEntity = userService.getById(userId);
        PayDetails payDetails = payDetailsService.getOne(new QueryWrapper<PayDetails>().eq("order_id", orderId));
        if (postPush.getStatus()==2||postPush.getStatus()==4) {

            if (userEntity.getIsUserVip() == 0) {//非vip
                if (payDetails.getState() != 1) {
                    return Result.error("请完成支付");
                } else {
                    postPush.setIsDue(1);
                    postPush.setOrderId(Math.toIntExact(orderId));
                    baseMapper.updateById(postPush);
                    return Result.success("发布为急聘成功");
                }
            } else {//vip
                if (userEntity.getVipDueTimes() == 0) {//没有免费次数
                    if (payDetails.getState() != 1) {
                        return Result.error("请完成支付");
                    } else {
                        postPush.setUpdateTime(String.valueOf(LocalDateTime.now()));
                        postPush.setIsDue(1);
                        postPush.setOrderId(Math.toIntExact(orderId));
                        baseMapper.updateById(postPush);
                        return Result.success("发布为急聘成功");
                    }
                } else {//有免费次数
                    userEntity.setVipDueTimes(userEntity.getVipPostTimes() - 1);
                    postPush.setIsDue(1);
                    postPush.setUpdateTime(String.valueOf(LocalDateTime.now()));
                    baseMapper.updateById(postPush);
                    return Result.success("发布为急聘成功");
                }
            }
        }else {
            return Result.error("当前岗位状态不能发布急聘");
        }
    }

    private void sendPostMeg(UserEntity userEntity, PostPush postPush) {
        CommonInfo tempId = commonInfoService.findOne(338);
        List<String> msgList = new ArrayList<>();
        msgList.add(postPush.getProvince() + "" + postPush.getCity() + "" + postPush.getCounty());
        RuleClassify ruleClassify = ruleClassifyDao.selectById(postPush.getRuleClassifyId());
        msgList.add(ruleClassify.getRuleClassifyName());
        switch (postPush.getStatus()) {
            case 1:
                msgList.add("待审核");
                break;
            case 2:
                msgList.add("审核通过");
                break;
            case 3:
                msgList.add("审核未通过");
                msgList.add("支付金额已退款");
                break;
            case 4:
                msgList.add("已取消");
                msgList.add("支付金额已退款");
                break;
            default:
                break;
        }
        if (msgList.size() == 3) {
            msgList.add("点击查看订单详情");
        }
        SenInfoCheckUtil.sendMsg(userEntity.getOpenId(), tempId.getValue(), msgList, 2, postPush.getPostPushId());
    }

    @Override
    public Result deletePostPush(Long userId, Long postPushId) {
        reentrantReadWriteLock.writeLock().lock();
        try {
            PostPush postPush = baseMapper.selectById(postPushId);
            Company company = companyService.selectCompanyByCompanyId(postPush.getCompanyId());
            if (company != null && (postPush.getStatus() == 1 || postPush.getStatus() == 3)) {
                if (!company.getUserId().equals(userId)) {
                    return Result.error("你无权操作");
                }
                String format = DateUtils.format(new Date());
                userMoneyService.updateMoney(1, company.getUserId(), postPush.getPayMoney());
                UserMoneyDetails userMoneyDetails = new UserMoneyDetails();
                userMoneyDetails.setMoney(postPush.getPayMoney());
                userMoneyDetails.setUserId(company.getUserId());
                userMoneyDetails.setContent("退款：" + postPush.getPayMoney());
                userMoneyDetails.setType(1);
                userMoneyDetails.setCreateTime(format);
                userMoneyDetailsService.save(userMoneyDetails);
            }
            browseService.remove(new QueryWrapper<UserBrowse>().eq("post_push_id", postPushId));
            myCollectionService.remove(new QueryWrapper<MyCollection>().eq("post_push_id", postPushId));
            baseMapper.deleteById(postPushId);
            userBrowseService.remove(new QueryWrapper<UserBrowse>().eq("post_push_id", postPushId));
            return Result.success();
        } catch (Exception e) {
            log.error("删除异常：" + e.getMessage(), e);
        } finally {
            reentrantReadWriteLock.writeLock().unlock();
        }
        return Result.success();
    }

    @Override
    public Result auditPostPush(String ids, String content, Integer status) {
        String format = DateUtils.format(new Date());
        for (String id : ids.split(",")) {
            PostPush postPush = baseMapper.selectById(Long.parseLong(id));
            Company company = companyService.selectCompanyByCompanyId(postPush.getCompanyId());
            if (postPush.getStatus() == 1) {
                if (status == 1) {
                    postPush.setStatus(2);
                    CommonInfo commonInfo = commonInfoService.findOne(326);
                    postPush.setOverdueTime(postPush.getCreateTime().plusDays(Long.parseLong(commonInfo.getValue())));
                } else {
                    postPush.setStatus(3);
                    postPush.setAuditContent(content);
                    userMoneyService.updateMoney(1, company.getUserId(), postPush.getPayMoney());
                    UserMoneyDetails userMoneyDetails = new UserMoneyDetails();
                    userMoneyDetails.setMoney(postPush.getPayMoney());
                    userMoneyDetails.setUserId(company.getUserId());
                    userMoneyDetails.setContent("退款：" + postPush.getPayMoney());
                    userMoneyDetails.setTitle("岗位发布未通过审核");
                    userMoneyDetails.setClassify(1);
                    userMoneyDetails.setType(1);
                    userMoneyDetails.setCreateTime(format);
                    userMoneyDetailsService.save(userMoneyDetails);
                }
                UserEntity userEntity = userService.getById(company.getUserId());
                if (StringUtils.isNotBlank(userEntity.getOpenId())) {
                    sendPostMeg(userEntity, postPush);
                }
                baseMapper.updateById(postPush);
            }
        }
        return Result.success();
    }


    @Override
    public Result getPostPushList(Integer page, Integer limit, Double lng, Double lat, Long userId, PostPush postPush, Integer screen,Integer isDue) {
        Page<PostPush> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        String[] industry = StringUtils.isNotBlank(postPush.getIndustry()) ? postPush.getIndustry().split(",") : null;
        String[] education = StringUtils.isNotBlank(postPush.getEducation()) ? postPush.getEducation().split(",") : null;
        String[] experience = StringUtils.isNotBlank(postPush.getExperience()) ? postPush.getExperience().split(",") : null;
        String[] companyPeople = StringUtils.isNotBlank(postPush.getCompanyPeople()) ? postPush.getCompanyPeople().split(",") : null;
        String[] salaryRange = StringUtils.isNotBlank(postPush.getSalaryRange()) ? postPush.getSalaryRange().split(",") : null;

        IPage<PostPush> selectPage = baseMapper.getPostPushList(pages, lng, lat, postPush, industry, education, experience, companyPeople, salaryRange,screen,isDue);
        List<PostPush> records = selectPage.getRecords();
        for (PostPush record : records) {
            Company company = companyService.selectCompanyByCompanyId(record.getCompanyId());
            record.setCompany(company);
            record.setUser(userService.getById(record.getUserId()));
            record.setHr(hrService.getByUserId(record.getUserId()));
        }
        return Result.success().put("data", selectPage);
    }

    @Override
    public List<LetterCity> getCityList(String city) {
        List<LetterCity> letterCityList = baseMapper.getLetter();
        for (LetterCity letterCity : letterCityList) {
            letterCity.setCity(baseMapper.getCityList(letterCity.getLetter(), city));
        }
        return letterCityList;
    }

    @Override
    public List<String> getCountyByCity(String city) {
        return baseMapper.getCountyByCity(city);
    }

    @Override
    public List<String> getHotCity(Integer limitCount) {
        return baseMapper.getHotCity(limitCount);

    }

    @Override
    public List<CityVo> getCityCounty() {
        List<CityVo> cityVos = baseMapper.getCityCounty();
        for (CityVo cityVo : cityVos) {
            cityVo.setCountyList(baseMapper.getCountyByCity(cityVo.getCity()));
        }
        return cityVos;
    }

    @Override
    public List<String> getCityCompanyId(Long companyId) {

        return baseMapper.getCityCompanyId(companyId);
    }

    @Override
    public List<String> getCompanyClassify(Long companyId) {
        return baseMapper.getCompanyClassify(companyId);

    }

    @Override
    public Result refreshPostPush(Long userId) {
        UserEntity userEntity = userService.getById(userId);
        Company company = companyService.getOne(new QueryWrapper<Company>().eq("user_id", userId));
        if (userEntity.getCompanyRefreshCount() > 0) {
            List<PostPush> pushList = baseMapper.selectList(new QueryWrapper<PostPush>().eq("company_id", company.getCompanyId()));
            for (PostPush postPush : pushList) {
                postPush.setUpdateTime(DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
                baseMapper.updateById(postPush);
            }
            userEntity.setCompanyRefreshCount(userEntity.getCompanyRefreshCount() - 1);
            userService.updateById(userEntity);
            return Result.success();
        } else {
            return Result.error("今日可刷新次数已用完");
        }


    }

    @Override
    public IPage<PostPush> getAdminPostPushList(Integer page, Integer limit, PostPush postPush, String startTime, String endTime) {

        Page<PostPush> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        return baseMapper.getAdminPostPushList(pages,postPush,startTime,endTime);
    }

    @Override
    public List<PostPush> listPostPushByCompanyId(Long companyId) {
        QueryWrapper<PostPush> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(PostPush::getCompanyId, companyId)
                .eq(PostPush::getStatus, 2);

        List<PostPush> postPushList = this.list(queryWrapper);
        return postPushList;
    }

    @Override
    public List<Object> getHotPost() {
        return baseMapper.getHotPost();
    }


    /*@Override
    public PostPush selectPostPushDetails(Long userId, Long postPushId, Double lng, Double lat) {
        PostPush postPush = baseMapper.selectPostId(userId, postPushId, lng, lat);
        if (postPush != null) {
            Company company = companyService.selectCompanyByCompanyId(postPush.getCompanyId());
            if (company != null) {
                if (userId != null && !company.getUserId().equals(userId)) {
                    int count = userBrowseService.count(new QueryWrapper<UserBrowse>().eq("user_id", userId).eq("post_push_id", postPushId));
                    if (count == 0) {
                        UserBrowse userBrowse = new UserBrowse();
                        userBrowse.setUserId(userId);
                        userBrowse.setBrowseType(1);
                        userBrowse.setPostPushId(postPushId);
                        userBrowse.setPostPushCompanyId(company.getCompanyId());
                        userBrowse.setUpdateTime(LocalDateTime.now());
                        userBrowseService.save(userBrowse);
                    }
                }
                postPush.setCompany(companyService.selectCompanyByUserId(company.getUserId()));
                postPush.setUser(userService.selectUserById(company.getUserId()));
            }
        }


        return postPush;
    }*/
    @Override
    public PostPush selectPostPushDetails(Long userId, Long postPushId, Double lng, Double lat) {
        PostPush postPush = baseMapper.selectById(postPushId);
        Company company = companyService.getById(postPush.getCompanyId());
        Long a = Long.valueOf(postPush.getUserId());
            if (a.equals(userId)){
                int count = userBrowseService.count(new QueryWrapper<UserBrowse>().eq("user_id", userId).eq("post_push_id", postPushId));
                if (count == 0) {
                    UserBrowse userBrowse = new UserBrowse();
                    userBrowse.setUserId(userId);
                    userBrowse.setBrowseType(1);
                    userBrowse.setPostPushId(postPushId);
                    userBrowse.setPostPushCompanyId(company.getCompanyId());
                    userBrowse.setUpdateTime(LocalDateTime.now());
                    userBrowseService.save(userBrowse);
                }
                postPush.setCompany(company);
                postPush.setUser(userService.getById(postPush.getUserId()));
                postPush.setHr(hrService.getByUserId(postPush.getUserId()));
            }
        return postPush;
    }

    @Override
    public Result closeOrOpen(Long userId, Long postPushId) {
        PostPush postPush = baseMapper.selectById(postPushId);
        Company company = companyService.getById(postPush.getCompanyId());
        if (userId.equals(company.getUserId())) {
            if (postPush.getStatus() != 2 && postPush.getStatus() != 5) {
                return Result.error("当前状态不允许修改");
            } else if (postPush.getStatus() == 2) {
                postPush.setStatus(5);
            } else if (postPush.getStatus() == 5) {
                postPush.setStatus(2);
            } else {
                return Result.error("参数错误");
            }
            if (postPush.getIsOverdue() == 1) {
                return Result.error("当前招聘信息已过期");
            }
            return baseMapper.updateById(postPush) > 0 ? Result.success() : Result.error();
        } else {
            return Result.error("你无权操作");
        }

    }

    @Override
    public Result cancelPostPush(Long userId, Long postPushId, int type) {
        PostPush postPush = baseMapper.selectById(postPushId);
        Company company = companyService.getById(postPush.getCompanyId());
        if (userId.equals(company.getUserId())) {
            if (type == 1) {
                return cancel(userId, postPush, company);
            } else {
                if (postPush.getStatus() == 1 || postPush.getStatus() == 3) {
                    return cancel(userId, postPush, company);
                } else {
                    return Result.error("当前状态无法取消");
                }
            }
        } else {
            return Result.error("你无权操作");
        }
    }

    private Result cancel(Long userId, PostPush postPush, Company company) {
        userMoneyService.updateMoney(1, userId, postPush.getPayMoney());
        postPush.setStatus(4);
        UserMoneyDetails userMoneyDetails = new UserMoneyDetails();
        userMoneyDetails.setMoney(postPush.getPayMoney());
        userMoneyDetails.setUserId(company.getUserId());
        userMoneyDetails.setClassify(1);
        userMoneyDetails.setType(1);
        userMoneyDetails.setTitle("取消招聘成功");
        userMoneyDetails.setTitle("取消招聘成功,退还钻石" + postPush.getPayMoney() + "个");
        userMoneyDetails.setCreateTime(DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
        userMoneyDetailsService.save(userMoneyDetails);
        UserEntity userEntity = userService.getById(company.getUserId());
        if (StringUtils.isNotBlank(userEntity.getOpenId())) {
            sendPostMeg(userEntity, postPush);
        }
        return baseMapper.updateById(postPush) > 0 ? Result.success() : Result.error();
    }
}
