package cn.superdesk.uniorder.service.order.base.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.unified.UnifiedOrderAppraise;
import cn.superdesk.uniorder.dao.entity.unified.UnifiedOrderGoodsAppraise;
import cn.superdesk.uniorder.dao.mapper.unified.UnifiedOrderAppraiseMapper;
import cn.superdesk.uniorder.dao.mapper.unified.UnifiedOrderGoodsAppraiseMapper;
import cn.superdesk.uniorder.dto.param.order.base.UnifiedOrderAppraiseParam;
import cn.superdesk.uniorder.dto.param.order.base.UnifiedOrderAppraiseQueryCriteria;
import cn.superdesk.uniorder.dto.vo.order.base.UnifiedOrderAppraiseHeadVo;
import cn.superdesk.uniorder.dto.vo.order.base.UnifiedOrderAppraiseListItemVo;
import cn.superdesk.uniorder.service.order.base.UnifiedOrderAppraiseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

@Service
public class UnifiedOrderAppraiseServiceImpl implements UnifiedOrderAppraiseService {


    @Autowired
    private UnifiedOrderAppraiseMapper      unifiedOrderAppraiseMapper;
//    @Autowired
//    private UnifiedOrderAppraiseMapperEx    unifiedOrderAppraiseMapperEx;
    @Autowired
    private UnifiedOrderGoodsAppraiseMapper unifiedOrderGoodsAppraiseMapper;


    /****************************************** extends BaseService 部分 start ******************************************/

    @Override
    public int insertSelective(UnifiedOrderAppraise entity) {
        return unifiedOrderAppraiseMapper.insertSelective(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(UnifiedOrderAppraise entity) {
        return unifiedOrderAppraiseMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {

        unifiedOrderAppraiseMapper.deleteByPrimaryKey(id);
    }

    @Override
    public UnifiedOrderAppraise findById(Long id) {

        return unifiedOrderAppraiseMapper.selectByPrimaryKey(id);
    }


    @Override
    public List<UnifiedOrderAppraise> findAll(Map<String, Object> queryMap) {

        return null;
    }

    @Override
    public PageInfo<UnifiedOrderAppraise> findByPaging(Map<String, Object> params, Integer pageNo, Integer pageSize) {

        return null;
    }

    /****************************************** extends BaseService 部分   end ******************************************/


    @Override
    public int addUnifiedOrderAppraise(UnifiedOrderAppraiseParam param, LoginSysUser loginSysUser) {

        DateTime now = DateUtil.date();

        UnifiedOrderAppraise forInsertEntity = new UnifiedOrderAppraise();
        AnBeanUtils.copy(param, forInsertEntity);

        forInsertEntity.setCreateTime(now);
        forInsertEntity.setCreateUserId(loginSysUser.getSysUserId());

//        forInsertEntity.setUpdateTime(now);
//        forInsertEntity.setUpdateUserId(loginSysUser.getSysUserId());

        return unifiedOrderAppraiseMapper.insertSelective(forInsertEntity);
    }

    @Override
    public int updateUnifiedOrderAppraise(UnifiedOrderAppraiseParam param, LoginSysUser loginSysUser) {

        UnifiedOrderAppraise originEntity = unifiedOrderAppraiseMapper.selectByPrimaryKey(param.getId());
        // AssertUtil.notNull(originEntity);
        if (ObjectUtil.isNull(originEntity)) {
            return 0;
        }

        DateTime now = DateUtil.date();

        UnifiedOrderAppraise forUpdateEntity = new UnifiedOrderAppraise();
        AnBeanUtils.copy(param, forUpdateEntity);

        forUpdateEntity.setId(originEntity.getId());
//        forUpdateEntity.setUpdateTime(now);
//        forUpdateEntity.setUpdateUserId(loginSysUser.getSysUserId());

        return unifiedOrderAppraiseMapper.updateByPrimaryKeySelective(forUpdateEntity);
    }

    @Override
    public int deleteUnifiedOrderAppraise(Long id, LoginSysUser loginSysUser) {
        DateTime now = DateUtil.date();
        return unifiedOrderAppraiseMapper.deleteByPrimaryKey(id);
    }


//    @Override
//    public UnifiedOrderAppraiseListItemVo findListItemVoById(Long id, LoginSysUser loginSysUser) {
//
//        List<UnifiedOrderAppraiseListItemVo> resultList = queryGenericParam(
//                UnifiedOrderAppraiseQueryCriteria
//                        .builder()
//                        .unifiedOrderAppraiseId(id)
//                        .build(),
//                loginSysUser);
//
//        if (CollUtil.isNotEmpty(resultList)) {
//            return resultList.get(0);
//        } else {
//            return null;
//        }
//    }

//    public R<List<UnifiedOrderAppraise>> pageQuery(Map<String, Object> conditions, int pageNun, int pageSize) {
//
//        UnifiedOrderAppraiseExample          example  = new UnifiedOrderAppraiseExample();
//        UnifiedOrderAppraiseExample.Criteria criteria = example.createCriteria();
//
//        // TODO 喜欢用Map 还是 Bean 自己喜欢... 后期添加 转 UnifiedOrderAppraiseExample.Criteria
//        // UnifiedOrderAppraise unifiedOrderAppraiseConditions = BeanUtils.mapToBean(conditions,UnifiedOrderAppraise.class);
//
//        PageHelper.startPage(pageNun, pageSize);
//        List<UnifiedOrderAppraise>     resultList = unifiedOrderAppraiseMapper.selectByExample(example);
//        PageInfo<UnifiedOrderAppraise> page       = new PageInfo<UnifiedOrderAppraise>(resultList);
//        PageHelper.clearPage();
//
//        return new R<List<UnifiedOrderAppraise>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), resultList);
//
//    }

//    public R<List<UnifiedOrderAppraiseListItemVo>> pageQueryGenericParam(
//            UnifiedOrderAppraiseQueryCriteria queryCriteria,
//            LoginSysUser loginSysUser) {
//
//        UnifiedOrderAppraiseExample          example  = new UnifiedOrderAppraiseExample();
//        UnifiedOrderAppraiseExample.Criteria criteria = example.createCriteria();
//
//        // TODO 添加条件... 后期添加 queryCriteria 转 UnifiedOrderAppraiseExample.Criteria
//
//        PageHelper.startPage(queryCriteria.getPageNumber(), queryCriteria.getPageSize());
//        List<UnifiedOrderAppraiseListItemVo>     resultList = queryGenericParam(queryCriteria, loginSysUser);
//        PageInfo<UnifiedOrderAppraiseListItemVo> page       = new PageInfo<UnifiedOrderAppraiseListItemVo>(resultList);
//        PageHelper.clearPage();
//
//        return new R<List<UnifiedOrderAppraiseListItemVo>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), resultList);
//
//    }

//    @Override
//    public UnifiedOrderAppraiseHeadVo queryHeadGenericParam(
//            UnifiedOrderAppraiseQueryCriteria queryCriteria,
//            LoginSysUser loginSysUser) {
//
//        return unifiedOrderAppraiseMapperEx.queryHeadGenericParam(queryCriteria);
//    }

//    @Override
//    public List<UnifiedOrderAppraiseListItemVo> queryGenericParam(
//            UnifiedOrderAppraiseQueryCriteria queryCriteria,
//            LoginSysUser loginSysUser) {
//
//        List<UnifiedOrderAppraiseListItemVo> resultList = unifiedOrderAppraiseMapperEx.queryGenericParam(queryCriteria);
//
//        additionalInfoQueryGenericParam(resultList);
//
//        return resultList;
//    }

    private void additionalInfoQueryGenericParam(List<UnifiedOrderAppraiseListItemVo> resultList) {

        if (CollUtil.isEmpty(resultList)) {
            return;
        }

        /**************************************** 操作人 start ****************************************/

//		List<Long> sysUserIds = new ArrayList<>();
//
//		List<Long> createUserIds = resultList.stream().filter(o -> o.getCreateUserId() != null).filter(AnLamdaUtils.distinctByKey(o -> o.getCreateUserId())).map(o -> o.getCreateUserId()).collect(Collectors.toList());
//		List<Long> updateUserIds = resultList.stream().filter(o -> o.getUpdateUserId() != null).filter(AnLamdaUtils.distinctByKey(o -> o.getUpdateUserId())).map(o -> o.getUpdateUserId()).collect(Collectors.toList());
//
//		sysUserIds.addAll(createUserIds);
//		sysUserIds.addAll(updateUserIds);
//		sysUserIds = sysUserIds
//				.stream()
//				.distinct()
//				.collect(Collectors.toList());
//
//		log.info("\n{}", JSONUtil.toJsonStr(sysUserIds));
//		Map<Long, SysUser> sysUserMap = sysUserService.convertSysUserIds2MapSysUserId2Entity(sysUserIds);

        /**************************************** 供应商 start ****************************************/

//		List<Long> outSaleIds = new ArrayList<>();
//
//		List<Long> outMerchantIds = resultList.stream().filter(o -> o.getOutMerchantId() != null).filter(AnLamdaUtils.distinctByKey(o -> o.getOutMerchantId())).map(o -> o.getOutMerchantId()).collect(Collectors.toList());
//
//		outSaleIds.addAll(outMerchantIds);
//		outSaleIds = outSaleIds
//				.stream()
//				.distinct()
//				.collect(Collectors.toList());
//
//		Map<Long, UnifiedSupplier> sysSupplierMap = sysSupplierService.convertOutSaleIds2MapOutSaleId2Entity(outSaleIds);

        for (UnifiedOrderAppraiseListItemVo vo : resultList) {


            /**************************************** 补-时间 start ****************************************/

//            CHINESE_DATE_PATTERN = "yyyy年MM月dd日";
//            CHINESE_DATE_TIME_PATTERN = "yyyy年MM月dd日HH时mm分ss秒";
//            NORM_DATE_PATTERN = "yyyy-MM-dd";
//            NORM_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

//			if (ObjectUtil.isNotNull(vo.getCreateTime())) {
//				vo.setCreateTimeFormat(DateUtil.date(vo.getCreateTime()).toString(DatePattern.NORM_DATETIME_PATTERN));
//			}
//			if (ObjectUtil.isNotNull(vo.getUpdateTime())) {
//				vo.setUpdateTimeFormat(DateUtil.date(vo.getUpdateTime()).toString(DatePattern.NORM_DATETIME_PATTERN));
//			}

            /**************************************** 补-来源 start ****************************************/

//			if (ObjectUtil.isNotNull(vo.getServiceNo())) {
//				vo.setServiceNoName(CommonServiceNoEnum.getByServiceNo(vo.getServiceNo()).getServiceName());
//			} else {
//				vo.setServiceNoName(CommonServiceNoEnum.UNKNOW.getServiceName());
//			}
//			if (ObjectUtil.isNotNull(vo.getSourceFrom())) {
//				vo.setSourceFromName(CommonSourceFromEnum.getBySourceFromCode(vo.getSourceFrom()).getSourceFromName());
//			} else {
//				vo.setSourceFromName(CommonSourceFromEnum.UNKNOW.getSourceFromName());
//			}

            /**************************************** 补-操作人 start ****************************************/

//			if (ObjectUtil.isNotNull(sysUserMap.get(vo.getCreateUserId()))) {
//				vo.setCreateUserName(sysUserMap.get(vo.getCreateUserId()).getFullname());
//			}
//			if (ObjectUtil.isNotNull(sysUserMap.get(vo.getUpdateUserId()))) {
//				vo.setUpdateUserName(sysUserMap.get(vo.getUpdateUserId()).getFullname());
//			}

        }

    }

    @Transactional
    @Override
    public void addObjSM(UnifiedOrderAppraise appraise, List<UnifiedOrderGoodsAppraise> goodsApp) {

        unifiedOrderAppraiseMapper.insert(appraise);
        for (UnifiedOrderGoodsAppraise item : goodsApp) {
            unifiedOrderGoodsAppraiseMapper.insert(item);
        }
    }

}
