package com.prolog.rdc.tms.app.service.business.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.prolog.common.util.ToolKits;
import com.prolog.framework.core.exception.BizException;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.core.restriction.Order;
import com.prolog.framework.core.restriction.Restriction;
import com.prolog.framework.core.restriction.Restrictions;
import com.prolog.product.core.framework.entity.UserEntity;
import com.prolog.product.core.framework.util.PlgFxUsrUtils;
import com.prolog.rdc.tms.app.constant.DataDictionaryConstant;
import com.prolog.rdc.tms.app.constant.SeriaNnumberConstant;
import com.prolog.rdc.tms.app.dao.mapper.TmsShipbillEvaluationMapper;
import com.prolog.rdc.tms.app.model.dto.business.ShipbillEvaluationDTO;
import com.prolog.rdc.tms.app.model.entity.TmsShipbillEvaluationModel;
import com.prolog.rdc.tms.app.model.vo.business.ShipbillEvaluationAddOrUpdateVO;
import com.prolog.rdc.tms.app.model.vo.business.ShipbillEvaluationQueryVO;
import com.prolog.rdc.tms.app.proxy.MainDataProxy;
import com.prolog.rdc.tms.app.service.business.ShipbillEvaluationService;
import com.prolog.rdc.tms.app.util.ConvertorUtil;
import com.prolog.rdc.tms.model.entity.SysRefdatadModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hongbo.pan
 * @date 2022/11/1
 */
@Service
public class ShipbillEvaluationServiceImpl implements ShipbillEvaluationService {

    @Autowired
    private TmsShipbillEvaluationMapper tmsShipbillEvaluationMapper;

    @Autowired
    private MainDataProxy mainDataProxy;

    @Autowired
    private ConvertorUtil convertorUtil;

    @Override
    public void addOrUpdate(ShipbillEvaluationAddOrUpdateVO vo) {
        if (StrUtil.isBlank(vo.getId())) {
            add(vo);
        } else {
            TmsShipbillEvaluationModel model = tmsShipbillEvaluationMapper.findById(vo.getId(), TmsShipbillEvaluationModel.class);
            if (ObjectUtil.isNull(model)) {
                throw new BizException("服务评价不存在，id=" + vo.getId());
            }
            update(vo, model);
        }
    }

    @Override
    public List<ShipbillEvaluationDTO> findEvaluation(ShipbillEvaluationQueryVO vo) {
        Criteria criteria = Criteria.forClass(TmsShipbillEvaluationModel.class);
        Restriction r1 = ToolKits.isEmpty(vo.getId()) ? null : Restrictions.eq(TmsShipbillEvaluationModel::getId, vo.getId());
        Restriction r2 = ToolKits.isEmpty(vo.getShipbillId()) ? null : Restrictions.eq(TmsShipbillEvaluationModel::getShipbillId, vo.getShipbillId());
        Restriction r3 = ToolKits.isEmpty(vo.getEvaluationTarget()) ? null : Restrictions.eq(TmsShipbillEvaluationModel::getEvaluationTarget, vo.getEvaluationTarget());
        Restriction r4 = ToolKits.isEmpty(vo.getProblemType()) ? null : Restrictions.eq(TmsShipbillEvaluationModel::getProblemType, vo.getProblemType());
        criteria.setRestriction(Restrictions.and(r1, r2, r3, r4));
        criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));
        List<TmsShipbillEvaluationModel> tmsShipbillEvaluationModelList = tmsShipbillEvaluationMapper.findByCriteria(criteria);
        List<ShipbillEvaluationDTO> shipbillEvaluationDTOList = Lists.newArrayList();
        if (CollectionUtil.isNotEmpty(tmsShipbillEvaluationModelList)) {
            List<SysRefdatadModel> dicData = convertorUtil.getDictionary(new String[] {
                    DataDictionaryConstant.EVALUATION_TARGET, // 评价对象
                    DataDictionaryConstant.EVALUATION_PROBLEM_TYPE // 问题类型
            });
            shipbillEvaluationDTOList = tmsShipbillEvaluationModelList.stream().map(tmsShipbillEvaluationModel -> {
                ShipbillEvaluationDTO dto = new ShipbillEvaluationDTO();
                BeanUtils.copyProperties(tmsShipbillEvaluationModel, dto);
                dto.setEvaluationTargetName(ConvertorUtil.getName(dicData, DataDictionaryConstant.EVALUATION_TARGET, String.valueOf(dto.getEvaluationTarget())));
                dto.setProblemTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.EVALUATION_PROBLEM_TYPE, String.valueOf(dto.getProblemType())));
                return dto;
            }).collect(Collectors.toList());
        }
        return shipbillEvaluationDTOList;
    }

    private void add(ShipbillEvaluationAddOrUpdateVO vo) {
        UserEntity user = PlgFxUsrUtils.getUserEntity();
        String id = mainDataProxy.getCodeNumberByCode(SeriaNnumberConstant.TMS_SHIPBILL_EVALUATION_ID);
        TmsShipbillEvaluationModel model = new TmsShipbillEvaluationModel();
        model.setId(id);
        model.setCode(id);
        model.setShipbillId(vo.getShipbillId());
        model.setShipbillNo(vo.getShipbillId());
        model.setEvaluationStar(vo.getEvaluationStar());
        model.setEvaluationTarget(vo.getEvaluationTarget());
        model.setProblemType(vo.getProblemType());
        model.setProblemDesc(vo.getProblemDesc());
        model.setPicUrl(vo.getPicUrl());
        model.setEvaluationUserId(user.getUserid());
        model.setEvaluationUserName(user.getNickname());
        model.setEvaluationUserType(user.getUserType());
        model.setEnterpriseId(user.getEnterpriseId());
        model.setEnterpriseName(user.getEnterpriseName());
        model.setCreateperson(user.getNickname());
        model.setCreatetime(new Date());
        model.setUpdateperson(user.getNickname());
        model.setUpdatetime(new Date());
        tmsShipbillEvaluationMapper.save(model);
    }

    private void update(ShipbillEvaluationAddOrUpdateVO vo, TmsShipbillEvaluationModel model) {
        UserEntity user = PlgFxUsrUtils.getUserEntity();
        model.setEvaluationStar(vo.getEvaluationStar());
        model.setEvaluationTarget(vo.getEvaluationTarget());
        model.setProblemType(vo.getProblemType());
        model.setProblemDesc(vo.getProblemDesc());
        model.setPicUrl(vo.getPicUrl());
        model.setUpdateperson(user.getNickname());
        model.setUpdatetime(new Date());
        tmsShipbillEvaluationMapper.update(model);
    }
}
