package com.heyu.api.controller.mm;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heyu.api.alibaba.request.mm.order.VVCommentAgreeRequest;
import com.heyu.api.alibaba.request.mm.order.VVCommentRejectRequest;
import com.heyu.api.alibaba.request.mm.order.VVCommentRequest;
import com.heyu.api.common.annotation.Describe;
import com.heyu.api.data.dao.vv.VvCommentDao;
import com.heyu.api.data.dao.vv.VvCommentDetailDao;
import com.heyu.api.data.dto.vv.VvCommentDTO;
import com.heyu.api.data.entity.vv.VvCommentDetailEntity;
import com.heyu.api.data.entity.vv.VvCommentEntity;
import com.heyu.api.data.enums.CommentStatusEnums;
import com.heyu.api.data.service.impl.AbstractCommentAction;
import com.heyu.api.data.utils.R;
import com.heyu.api.utils.ISelect;
import com.heyu.api.utils.PPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/mm/comment")
public class AdminCommentController {


    @Autowired
    private VvCommentDao vvCommentDao;

    @Autowired
    private VvCommentDetailDao vvCommentDetailDao;

    /***
     * 列表
     *
     * /mm/comment/list
     *
     *
     */
    @Describe("评论列表")
    @RequestMapping("/list")
    public R list(@RequestBody VVCommentRequest vvCommentRequest) {
        PPageUtils pageUtils = PPageUtils.startPage(vvCommentRequest.getPageNum(), vvCommentRequest.getPageSize())
                .doSelect(new ISelect() {
                    @Override
                    public List doSelect(IPage page) {
                        return vvCommentDao.selectVvCommentByCondition(page,
                                vvCommentRequest.getBuyerId(),
                                vvCommentRequest.getBuyerName(),
                                vvCommentRequest.getBuyerPhone(),
                                vvCommentRequest.getProductId(),
                                vvCommentRequest.getProductName(),
                                vvCommentRequest.getSkuId(),
                                vvCommentRequest.getTradeOrderId(),
                                vvCommentRequest.getStatus(),
                                vvCommentRequest.getProductComment(),
                                vvCommentRequest.getServiceComment(),
                                vvCommentRequest.getMinDescMatch(),
                                vvCommentRequest.getMaxDescMatch(),
                                vvCommentRequest.getMinSellerService(),
                                vvCommentRequest.getMaxSellerService(),
                                vvCommentRequest.getMinLogisticsService(),
                                vvCommentRequest.getMaxLogisticsService(),
                                vvCommentRequest.getMinUsefulCount(),
                                vvCommentRequest.getMaxUsefulCount(),
                                vvCommentRequest.getCreateTimestampSort()
                        );
                    }
                });
        List<VvCommentEntity> vvCommentEntities = pageUtils.getRows();
        List<VvCommentDTO> commentDTOS = new ArrayList<>();
        for (VvCommentEntity vvCommentEntity : vvCommentEntities) {
            VvCommentDTO vvCommentDTO = new VvCommentDTO();
            BeanUtils.copyProperties(vvCommentEntity, vvCommentDTO);
            vvCommentDTO.setCommentActionList(AbstractCommentAction.getOrderActionDTO(vvCommentEntity, false));
            List<VvCommentDetailEntity> vvCommentDetailEntities = vvCommentDetailDao.selectVvCommentDetailByCommentId(vvCommentDTO.getId());
            vvCommentDTO.setVvCommentDetailEntities(vvCommentDetailEntities);
            commentDTOS.add(vvCommentDTO);
        }
        pageUtils.setRows(commentDTOS);
        return R.ok().setData(pageUtils);
    }

    /***
     *
     */
    @Describe("评论保存")
    @RequestMapping("/insertOrUpdate")
    public R insert(@RequestBody VvCommentDTO vvCommentDTO) {
        Long commentId = vvCommentDTO.getId();
        VvCommentEntity vvCommentEntity = new VvCommentEntity();
        BeanUtils.copyProperties(vvCommentDTO, vvCommentEntity);
        vvCommentEntity.setIsSystem(1);
        vvCommentDao.insertOrUpdateVvComment(vvCommentEntity);
        if (commentId != null) {
            List<VvCommentDetailEntity> vvCommentDetailDB = vvCommentDetailDao.selectVvCommentDetailByCommentId(commentId);
            if (vvCommentDTO.getVvCommentDetailEntities() != null) {
                for (VvCommentDetailEntity commentDetailEntity : vvCommentDetailDB) {
                    boolean flag = true;
                    for (VvCommentDetailEntity vvCommentDetailEntity : vvCommentDTO.getVvCommentDetailEntities()) {
                        if (commentDetailEntity.getId().equals(vvCommentDetailEntity.getId())) {
                            flag = false;
                        }
                    }
                    if (flag) {
                        vvCommentDetailDao.deleteVvCommentDetailById(commentDetailEntity.getId());
                    }
                }
            }
        }

        if (CollectionUtils.isNotEmpty(vvCommentDTO.getVvCommentDetailEntities())) {
            for (VvCommentDetailEntity vvCommentDetailEntity : vvCommentDTO.getVvCommentDetailEntities()) {
                vvCommentDetailEntity.setCommentId(vvCommentEntity.getId());
                vvCommentDetailEntity.setProductId(vvCommentEntity.getProductId());
                vvCommentDetailDao.insertOrUpdateVvCommentDetail(vvCommentDetailEntity);
            }
        }
        return R.ok();
    }


    /***
     * /mm/comment/agree
     */
    @Describe("同意评论")
    @RequestMapping("/agree")
    public R agree(@RequestBody VVCommentAgreeRequest vvCommentRequest) {
        VvCommentEntity vvCommentEntity = vvCommentDao.selectVvCommentById(vvCommentRequest.getId());
        vvCommentEntity.setStatus(CommentStatusEnums.approval_pass.getStatus());
        vvCommentEntity.setGmtAgree(new Date());
        vvCommentEntity.setAgreeUser(vvCommentRequest.getUserName());
        vvCommentDao.updateVvCommentById(vvCommentEntity);
        return R.ok();
    }

    /***
     * /mm/comment/reject
     */
    @Describe("拒绝评论")
    @RequestMapping("/reject")
    public R reject(@RequestBody VVCommentRejectRequest vvCommentRequest) {
        VvCommentEntity vvCommentEntity = vvCommentDao.selectVvCommentById(vvCommentRequest.getId());
        vvCommentEntity.setStatus(CommentStatusEnums.approval_not_pass.getStatus());
        vvCommentEntity.setRejectUser(vvCommentRequest.getUserName());
        vvCommentEntity.setReason(vvCommentRequest.getReason());
        vvCommentDao.updateVvCommentById(vvCommentEntity);
        return R.ok();
    }
}
