package org.linlinjava.jz.admin.service;

import com.github.pagehelper.PageHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.jz.db.dao.CustomerOrderMapper;
import org.linlinjava.jz.db.domain.CustomerOrder;
import org.linlinjava.jz.db.domain.HmComment;
import org.linlinjava.jz.db.domain.HmEmployClock;
import org.linlinjava.jz.db.domain.HmEmployRelation;
import org.linlinjava.jz.db.service.hm.HmCommentService;
import org.linlinjava.jz.db.service.hm.HmEmployRelationService;
import org.linlinjava.jz.db.util.hm.HmCommentEnum;
import org.linlinjava.jz.service.HmEmployClockService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 客户订单管理
 */
@Service
public class AdminCustomerOrderService {
    private final Log logger = LogFactory.getLog(AdminCustomerService.class);
    @Resource
    private CustomerOrderMapper customerOrderMapper;
    @Resource
    private HmEmployClockService hmEmployClockService;
    @Resource
    private HmEmployRelationService hmEmployRelationService;
    @Resource
    public HmCommentService hmCommentService;

    public List<Map<String, Object>> list(String customerName, String customerTel, Long userId,Integer page, Integer limit) {
        CustomerOrder customerOrder = new CustomerOrder();
        customerOrder.setCustomerName(customerName);
        customerOrder.setCustomerTel(customerTel);
		customerOrder.setUserId(userId);
        PageHelper.startPage(page, limit);
        return customerOrderMapper.selectOrderList(customerOrder);
    }

    public CustomerOrder selectById(Long id) {
        return customerOrderMapper.selectByPrimaryKey(id);
    }

    public List<HmEmployClock> queryTaskClockList(String orderNo,Long clockId, Integer page, Integer limit) {
        List<HmEmployClock> hmEmployClocks = hmEmployClockService.queryByOrderNo(orderNo,clockId, page, limit);
        hmEmployClocks.forEach(item -> {
            HmEmployRelation hmEmployRelation = hmEmployRelationService.queryById(item.getEmployRelationId());
            if (null != hmEmployRelation) {
                item.setServantName(hmEmployRelation.getServantName());
            }
            HmComment hmComment = hmCommentService.queryByRIdAndType(item.getId(),HmCommentEnum.comment_num_3.getType());
            if (null != hmComment) {
                item.setStar(hmComment.getStar());
            }
        });
        return hmEmployClocks;
    }

    public HmEmployClock queryById(Long id) {
        return hmEmployClockService.queryById(id);
    }
}
