package com.yh.saas.plugin.yiliangyiyun.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.winsea.svc.base.security.util.AuthSecurityUtils;
import com.yh.saas.common.support.util.IdGenerator;
import com.yh.saas.plugin.yiliangyiyun.constant.StatusEnum;
import com.yh.saas.plugin.yiliangyiyun.entity.*;
import com.yh.saas.plugin.yiliangyiyun.exception.ServiceException;
import com.yh.saas.plugin.yiliangyiyun.mapper.SettledCompanyDynamicsMapper;
import com.yh.saas.plugin.yiliangyiyun.service.ICommonUserService;
import com.yh.saas.plugin.yiliangyiyun.service.ISettledCompanyDynamicsService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yh.saas.plugin.yiliangyiyun.util.GeTuiUtils;
import com.yh.saas.plugin.yiliangyiyun.util.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.SimpleIdGenerator;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 入驻公司发布动态 服务实现类
 * </p>
 *
 * @author Gongdc
 * @since 2021-08-28
 */
@Service
public class SettledCompanyDynamicsServiceImpl extends ServiceImpl<SettledCompanyDynamicsMapper, SettledCompanyDynamics> implements ISettledCompanyDynamicsService {

    @Autowired
    private GeTuiUtils geTuiUtils;
    @Autowired
    private ICommonUserService commonUserService;
    @Autowired
    private WebSocket webSocket;


    /**
     * 发布动态列表
     *
     * @param settledCompanyDynamics
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Page<SettledCompanyDynamics> selectSettledCompanyDynamicsPage(SettledCompanyDynamics settledCompanyDynamics) {
        Map<String, Object> pageView = new HashMap<>();
        pageView.put("startRecord", (settledCompanyDynamics.getCurrentPage() - 1)
                * settledCompanyDynamics.getPageSize());
        //公司id
        pageView.put("loginPhone", settledCompanyDynamics.getPhone());
        pageView.put("searchKeyWord", settledCompanyDynamics.getSearchKeyWord());
        pageView.put("pcFlag", settledCompanyDynamics.getPcFlag());
        pageView.put("searchType", settledCompanyDynamics.getSearchType());
        pageView.put("pageSize", settledCompanyDynamics.getPageSize());
        pageView.put("currentPage", settledCompanyDynamics.getCurrentPage());
        // 查询总数
        Integer dataCount = baseMapper.getCountByCondition(pageView);
        List<SettledCompanyDynamics> dataList = baseMapper.getListByCondition(pageView);
        if (!CollectionUtils.isEmpty(dataList)) {
            for (SettledCompanyDynamics settledCompanyDynamics1 : dataList) {
                CommonUser commonUser = commonUserService.selectOne(new EntityWrapper<CommonUser>().eq("phone", settledCompanyDynamics1.getPhone()));
                if (commonUser != null) {
                    //昵称
                    settledCompanyDynamics1.setUserName(commonUser.getUserName());
                    //头像
                    settledCompanyDynamics1.setAvatarUrl(commonUser.getAvatarUrl()!=null?commonUser.getAvatarUrl():"");
                }
            }
        }
        Page<SettledCompanyDynamics> page = new Page<>();
        page.setRecords(dataList == null ? Lists.newArrayList() : dataList);
        page.setTotal(dataCount == null ? 0 : dataCount);
        page.setCurrent(settledCompanyDynamics.getCurrentPage());
        page.setSize(settledCompanyDynamics.getPageSize());
        return page;
    }


    /**
     * 发布动态
     *
     * @param settledCompanyDynamics
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addSettledCompanyDynamics(SettledCompanyDynamics settledCompanyDynamics) throws ServiceException {
        settledCompanyDynamics.setId(IdGenerator.generateUUID());
        settledCompanyDynamics.setStatusFlag(StatusEnum.TO_BE_REVIEWED.getFlag());
        settledCompanyDynamics.setStatus(StatusEnum.TO_BE_REVIEWED.getName());
        settledCompanyDynamics.setShowFlag("1");
        this.insert(settledCompanyDynamics);
        return settledCompanyDynamics.getId();
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSettledCompanyDynamics(String id) {
        //查询信息
        SettledCompanyDynamics settledCompanyInfo = this.selectById(id);
        if (settledCompanyInfo != null) {
            //删除信息
            this.deleteById(settledCompanyInfo.getId());
        }
    }


    /**
     * 审核
     *
     * @param settledCompanyDynamics
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String examine(SettledCompanyDynamics settledCompanyDynamics) {
        //查询
        SettledCompanyDynamics settledCompanyDynamics1 = this.selectById(settledCompanyDynamics.getId());
        CommonUser commonUser = commonUserService.selectOne(new EntityWrapper<CommonUser>().eq("phone", settledCompanyDynamics1.getPhone()));
        if (settledCompanyDynamics1 != null) {
            //通过
            if (settledCompanyDynamics.getFlag() == 1) {
                settledCompanyDynamics1.setStatusFlag(StatusEnum.ORDER_PASSED.getFlag());
                settledCompanyDynamics1.setStatus(StatusEnum.ORDER_PASSED.getName());
                geTuiUtils.pushByCid("审核通知", "您发布的动态已审核通过", commonUser.getId());

                UnimallMessage messageDO = new UnimallMessage();
                messageDO.setAdminId(commonUser.getId());
                messageDO.setCustomer(commonUser.getUserName());
                messageDO.setOperation("审核通知");
                messageDO.setResult("您发布的动态已审核通过");
                messageDO.setGmtUpdate(new java.util.Date());
                messageDO.setGmtCreate(new Date());
                messageDO.setTaskId(IdGenerator.generateUUID());
                messageDO.setCompanyId(AuthSecurityUtils.getCurrentUserInfo().getCompId());
                messageDO.setPath("XXXXX");
                webSocket.sendOneMessage(messageDO);
            }
            //驳回
            else {
                settledCompanyDynamics1.setStatusFlag(StatusEnum.ORDER_REJECTED.getFlag());
                settledCompanyDynamics1.setStatus(StatusEnum.ORDER_REJECTED.getName());
                geTuiUtils.pushByCid("驳回通知", "您发布的动态已被驳回", commonUser.getId());
                UnimallMessage messageDO = new UnimallMessage();
                messageDO.setAdminId(commonUser.getId());
                messageDO.setCustomer(commonUser.getUserName());
                messageDO.setOperation("驳回通知");
                messageDO.setResult("您发布的动态已被驳回");
                messageDO.setGmtUpdate(new java.util.Date());
                messageDO.setGmtCreate(new Date());
                messageDO.setTaskId(IdGenerator.generateUUID());
                messageDO.setCompanyId(AuthSecurityUtils.getCurrentUserInfo().getCompId());
                messageDO.setPath("XXXXX");
                webSocket.sendOneMessage(messageDO);
            }
            //更改信息
            this.updateById(settledCompanyDynamics1);
            return "OK";
        }
        return "NG";
    }


    /**
     * 显示隐藏
     *
     * @param id
     * @return
     */
    @Override
    public void showHidden(String id) {
        //查询销售计划信息
        SettledCompanyDynamics settledCompanyDynamics = this.selectById(id);
        if (settledCompanyDynamics != null) {
            if ("2".equals(settledCompanyDynamics.getShowFlag())) {
                settledCompanyDynamics.setShowFlag("1");
            } else {
                settledCompanyDynamics.setShowFlag("2");
            }
            this.updateById(settledCompanyDynamics);
        }
    }

    /**
     * 查看详情
     *
     * @param id
     * @return
     */
    @Override
    public SettledCompanyDynamics getSettledCompanyDynamics(String id) {
        //查看信息
        SettledCompanyDynamics settledCompanyDynamics = this.selectById(id);
        CommonUser commonUser = commonUserService.selectOne(new EntityWrapper<CommonUser>().eq("phone", settledCompanyDynamics.getPhone()));
        if (commonUser != null) {
            //头像
            settledCompanyDynamics.setAvatarUrl(commonUser.getAvatarUrl()!=null?commonUser.getAvatarUrl():"");
        }
        return settledCompanyDynamics;
    }

    /**
     * 查看当天发布条数
     *
     * @param phone
     * @return
     */
    @Override
    public Integer count(String phone) {
        //当天零点
        SimpleDateFormat now = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.YEAR, 0);
        Date y = c.getTime();
        String year = now.format(y);
        Integer count = this.selectCount(new EntityWrapper<SettledCompanyDynamics>()
                .eq("phone", phone)
                .ge("create_date", year));
        return count;
    }

}
