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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
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.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.artificer.entity.MassageType;
import com.sqx.modules.artificer.entity.Orders;
import com.sqx.modules.artificer.service.MassageTypeService;
import com.sqx.modules.artificer.service.OrdersService;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.material.entity.MaterialArtificerResult;
import com.sqx.modules.risk.entity.Risk;
import com.sqx.modules.risk.service.RiskService;
import com.sqx.modules.taking.dao.OrderTakingCommentDao;
import com.sqx.modules.taking.entity.CommentFabulous;
import com.sqx.modules.taking.entity.TakingCommnt;
import com.sqx.modules.taking.response.TakingDetailsResponse;
import com.sqx.modules.taking.service.OrderTakingCommentService;
import com.sqx.modules.utils.SenInfoCheckUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Service
public class OrderTakingCommentServiceImpl extends ServiceImpl<OrderTakingCommentDao, TakingCommnt> implements OrderTakingCommentService {

    @Autowired
    private OrderTakingCommentDao orderTakingCommentDao;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private MassageTypeService massageTypeService;
    @Autowired
    private RiskService riskService;
    @Autowired
    private CommonInfoService commonInfoService;

    @Override
    public Result selectOrderTakingComment(Integer page, Integer limit, Long artificerId,Long massageTypeId) {
        Page<TakingDetailsResponse> page1 = new Page<>(page, limit);
        IPage iPage = orderTakingCommentDao.selectOrderTakingComment(page1, artificerId,massageTypeId);
        return Result.success().put("data", new PageUtils(iPage));
    }

    @Override
    public Result selectOrderTakingCommentByUserId(Integer page, Integer limit, Long userId) {
        Page<Map<String,Object>> page1 = new Page<>(page, limit);
        IPage<Map<String,Object>> iPage = orderTakingCommentDao.selectOrderTakingCommentByUserId(page1, userId);
        return Result.success().put("data", new PageUtils(iPage));
    }

    @Override
    public Result updateGoodsNum(Long commentId, Long userId) {
        //判断自己是否点过赞
        CommentFabulous commentFabulous = orderTakingCommentDao.selectGoodsNum(commentId, userId);
        if (commentFabulous != null) {
            //有赞则取消点赞
            int i = orderTakingCommentDao.deleteGoodsNum(commentFabulous.getId());
            if (i > 0) {
                return Result.success("取消点赞成功！");
            } else {
                return Result.error("取消点赞失败！");
            }
        } else {
            //无赞 点赞
            int i = orderTakingCommentDao.insertGoodsNum(commentId, userId);
            if (i > 0) {
                return Result.success("点赞成功！");
            } else {
                return Result.error("点赞失败！");
            }
        }
    }

    @Override
    public Result addGoodsNum(Long orderId, Long userId, String content, Integer score,Long artificerId) {
        int i = selectTakingCommentCount(orderId, userId);
        if(i>0){
            return Result.error("您已经评价过了！");
        }
        Orders orders = ordersService.getById(orderId);
        if(!orders.getStatus().equals(3)){
            return Result.success("当前订单未完成或已经评价过了！");
        }
        String value = commonInfoService.findOne(323).getValue();
        if(value.contains(content)){
            Risk risk=new Risk();
            risk.setUserId(userId);
            risk.setContent("系统检测触发关键词："+content);
            risk.setRiskType(4);
            risk.setCreateTime(DateUtils.format(new Date()));
            riskService.save(risk);
            return Result.error(-200,"内容存在违规信息，请检查后重新提交！");
        }
        if(!SenInfoCheckUtil.contentFilter(content)){
            Risk risk=new Risk();
            risk.setUserId(userId);
            risk.setContent("系统检测触发关键词："+content);
            risk.setRiskType(4);
            risk.setCreateTime(DateUtils.format(new Date()));
            riskService.save(risk);
            return Result.error(-200,"内容存在违规信息，请检查后重新提交！");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //创建
        TakingCommnt takingCommnt = new TakingCommnt();
        takingCommnt.setContent(content);
        takingCommnt.setOrderId(orderId);
        takingCommnt.setUserId(userId);
        takingCommnt.setCreateTime(simpleDateFormat.format(new Date()));
        takingCommnt.setScore(score);
        takingCommnt.setArtificerId(artificerId);
        takingCommnt.setMassageTypeId(orders.getMassageTypeId());
        baseMapper.insert(takingCommnt);
        orders.setStatus(5);
        ordersService.updateById(orders);
        //修改订单下所有加钟的状态为已完成
        UpdateWrapper<Orders> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("parent_id", orders.getOrdersId());
        Orders orders2 = new Orders();
        orders2.setStatus(5);
        //修改为订单已完成
        ordersService.update(orders2,updateWrapper);
        if(orders.getMassageTypeId()!=null){
            MassageType massageType = massageTypeService.getById(orders.getMassageTypeId());
            if(massageType!=null){
                if(StringUtils.isEmpty(massageType.getScore())){
                    massageType.setScore(String.valueOf(score));
                }else{
                    BigDecimal add = new BigDecimal(massageType.getScore()).add(BigDecimal.valueOf(score));
                    massageType.setScore(add.toString());
                }
                massageTypeService.updateById(massageType);
            }
        }


        return Result.success();
    }

    @Override
    public int selectTakingCommentCount(Long ordersId,Long userId){
        return baseMapper.selectCount(new QueryWrapper<TakingCommnt>().eq("order_id",ordersId).eq("user_id",userId));
    }

    @Override
    public Result selectTakingCommentByArtificerId(Integer page, Integer limit, Long artificerId) {
        IPage<TakingCommnt> page1 = new Page<>(page, limit);
        return Result.success().put("data", baseMapper.selectTakingCommentByArtificerId(page1,artificerId));
    }


}
