package org.linlinjava.jz.db.service;

import org.linlinjava.jz.db.dao.CustomerClientMapper;
import org.linlinjava.jz.db.dao.CustomerOrderMapper;
import org.linlinjava.jz.db.domain.*;
import org.linlinjava.jz.db.dto.hm.HmUserOrderInfo;
import org.linlinjava.jz.db.dto.hm.HmUserServiceInfo;
import org.linlinjava.jz.db.service.hm.HmAppUserService;
import org.linlinjava.jz.db.service.hm.HmEmployRelationService;
import org.linlinjava.jz.db.service.hm.HmEmployTaskSetService;
import org.linlinjava.jz.db.util.HmResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @author fuxubing
 */
@Service
public class CustomerOrderService {
    @Resource
    private CustomerOrderMapper customerOrderMapper;

    @Autowired
    private HmEmployRelationService employRelationService;

    @Autowired
    private HmAppUserService appUserService;

    @Autowired
    private CustomerClueService clueService;

    @Autowired
    private CustomerClientMapper customerClientMapper;

    @Autowired
    private ContractSubTemplateService contractSubTemplateService;

    @Autowired
    private HmEmployTaskSetService employTaskSetService;

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

    public CustomerOrder selectByOrderNo(String orderNo) {

        CustomerOrderExample example = new CustomerOrderExample();
        example.or().andOrderNoEqualTo(orderNo);

        return customerOrderMapper.selectOneByExample(example);
    }

    /**
     * 管家 详细信息里的--订单信息，服务信息
     *
     * @param employRelationId
     * @return
     */
    public Object getOrderInfo(Long employRelationId) {
        HmEmployRelation relation = employRelationService.queryById(employRelationId);
        if (relation == null) {
            return HmResponseUtil.fail_null();
        }

        HmAppUser userServant = appUserService.queryById(relation.getUserServantId());
        HmAppUser userCustomer = appUserService.queryById(relation.getUserEmployerId());

        CustomerOrder customerOrder = this.selectByOrderNo(relation.getOrderNo());
        CustomerClue customerClue = clueService.selectByClueNo(customerOrder.getClueNo());
        CustomerClient customerClient = customerClientMapper.selectByPrimaryKey(customerClue.getCustomerClientId());

        ContractSubTemplate contractSubTemplate = contractSubTemplateService.selectByOrderNo(relation.getOrderNo());
        if (contractSubTemplate == null) {
            contractSubTemplate = new ContractSubTemplate();
        }

        HmUserOrderInfo order = new HmUserOrderInfo();
        order.setCustomerName(relation.getButlerName());
        order.setCustomerPhone(userCustomer.getPhone());
        order.setCustomerType(customerClient.getCustomerType());// 客户类型 0客户，1服务者、2合伙人、3其他
        order.setServantName(relation.getServantName());
        order.setServantPhone(userServant.getPhone());
        order.setServantType(customerClue.getServiceModel());// 服务模式1钟点工,2白班保姆,3住家保姆，4月嫂，5代班保姆-终点工，6代班保姆-保姆

        HmUserServiceInfo serviceInfo = new HmUserServiceInfo();
        serviceInfo.setCustomerName(relation.getEmployerName());
        serviceInfo.setCustomerPhone(userCustomer.getPhone());
        serviceInfo.setServantName(relation.getServantName());
        serviceInfo.setServiceAddress(contractSubTemplate.getServiceAddress());
        serviceInfo.setFirstServiceTime(contractSubTemplate.getServiceStartTime());
        serviceInfo.setOrderNo(relation.getOrderNo());

        if (contractSubTemplate.getMonthlySalary() != null) {
            serviceInfo.setSalaryType("月薪");
            serviceInfo.setSalaryAmount(contractSubTemplate.getMonthlySalary());
        }
        if (contractSubTemplate.getDaySalary() != null) {
            serviceInfo.setSalaryType("日薪");
            serviceInfo.setSalaryAmount(contractSubTemplate.getDaySalary());
        }
        if (contractSubTemplate.getHourlySalary() != null) {
            serviceInfo.setSalaryType("时薪");
            serviceInfo.setSalaryAmount(contractSubTemplate.getHourlySalary());
        }
        serviceInfo.setPayDate(contractSubTemplate.getPayday());
        serviceInfo.setHolidaySalaryMultiple(contractSubTemplate.getWorkerMultiple());
        serviceInfo.setOffMultiple(contractSubTemplate.getOffMultiple());
        serviceInfo.setRestDay(relation.getRestDay());
        serviceInfo.setIsNeedClock(relation.getIsNeedClock());

        List<Map<String, Object>> day = employTaskSetService.getDiffTypeTask(relation.getOrderNo(), 1);
        List<Map<String, Object>> week = employTaskSetService.getDiffTypeTask(relation.getOrderNo(), 2);
        List<Map<String, Object>> month = employTaskSetService.getDiffTypeTask(relation.getOrderNo(), 3);

        if (day.size() > 0) {
            serviceInfo.setEveryDayTask(getStrList(day));
        }
        if (week.size() > 0) {
            serviceInfo.setEveryMonthTask(getStrList(week));
        }
        if (month.size() > 0) {
            serviceInfo.setEveryWeekTask(getStrList(month));
        }

        serviceInfo.setDayTaskMap(day);
        serviceInfo.setWeekTaskMap(week);
        serviceInfo.setMonthTaskMap(month);

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("orderInfo", order);
        data.put("serviceInfo", serviceInfo);

        return HmResponseUtil.ok(data);
    }

    private String getStrList(List<Map<String, Object>> day) {
//		Collection<Object> list=new ArrayList<Object>();
        StringBuilder itemName = new StringBuilder();
        if (null != day && day.size() > 0) {
            for (Map<String, Object> r : day) {
                if (r.isEmpty()) {
                    continue;
                }
                itemName.append(r.get("items")).append(",");
            }
        }
        return itemName.deleteCharAt(itemName.length() - 1).toString();
    }
}
