package org.linlinjava.jz.hm.web.employer;

import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.jz.core.util.DateTimeUtil;
import org.linlinjava.jz.db.domain.ContractSubTemplate;
import org.linlinjava.jz.db.domain.HmEmployRelation;
import org.linlinjava.jz.db.service.ContractSubTemplateService;
import org.linlinjava.jz.db.service.CustomerOrderService;
import org.linlinjava.jz.db.service.hm.HmEmployRelationService;
import org.linlinjava.jz.db.util.HmResponseUtil;
import org.linlinjava.jz.hm.vo.DateTagVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fuxubing
 */
@Api(value = "保姆-服务信息", description = "保姆-服务信息")
@RestController
@RequestMapping("index")
@Valid
public class EmployerIndexController {
    @Resource
    private CustomerOrderService customerOrderService;
    @Resource
    private HmEmployRelationService hmEmployRelationService;
    @Resource
    private ContractSubTemplateService contractSubTemplateService;

    @ApiOperation(value = "保姆服务信息", notes = "保姆服务信息")
    @GetMapping(value = "getServiceInfo")
    public Object getServiceInfo(@NotNull @RequestParam Long employRelationId) {
        HmEmployRelation relation = hmEmployRelationService.queryById(employRelationId);
        if (relation == null) {
            return HmResponseUtil.fail_null();
        }
        return customerOrderService.getOrderInfo(employRelationId);
    }


    @ApiOperation(value = "雇主首次确认保姆服务信息", notes = "雇主首次确认保姆服务信息")
    @GetMapping(value = "confirm")
    public Object confirm(@NotNull @RequestParam Long employRelationId) {
        HmEmployRelation relation = hmEmployRelationService.queryById(employRelationId);
        if (relation == null) {
            return HmResponseUtil.fail_null();
        }
        relation.setButlerFirstConfirm(1);
        relation.setUpdateTime(LocalDateTime.now());
        hmEmployRelationService.update(relation);

        return HmResponseUtil.ok();
    }

    @ApiOperation(value = "查看休息日", notes = "查看休息日")
    @GetMapping(value = "getRestDay")
    public Object getRestDay(@NotNull @RequestParam Long employRelationId) {
        HmEmployRelation relation = hmEmployRelationService.queryById(employRelationId);
        if (relation == null) {
            return HmResponseUtil.fail_null();
        }

        //休息日
        List<DateTagVO> list = Lists.newArrayList();
        String restDay = relation.getRestDay();
        if (StringUtils.isNotBlank(restDay)) {
            String[] split = restDay.split(",");
            for (String s : split) {
                list.add(new DateTagVO(s, "休息日", 3));
            }
        }

        //合同模板信息
        ContractSubTemplate contractSubTemplate = contractSubTemplateService.selectByOrderNo(relation.getOrderNo());

        Map<String, Object> map = new HashMap<>(16);
        map.put("restDay", list);
        map.put("serviceStartTime", new DateTagVO(DateTimeUtil.localDateFormat(contractSubTemplate.getServiceStartTime(), "yyyy-MM-dd"), "服务开始日", 4));

        return HmResponseUtil.ok(map);
    }
}
