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

import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.linlinjava.jz.core.util.DateTimeUtil;
import org.linlinjava.jz.db.domain.ContractSub;
import org.linlinjava.jz.db.domain.ContractSubTemplate;
import org.linlinjava.jz.db.domain.HmAppUser;
import org.linlinjava.jz.db.domain.HmEmployRelation;
import org.linlinjava.jz.db.service.ContractSubService;
import org.linlinjava.jz.db.service.ContractSubTemplateService;
import org.linlinjava.jz.db.service.hm.HmAppUserService;
import org.linlinjava.jz.db.service.hm.HmEmployRelationService;
import org.linlinjava.jz.db.util.HmResponseUtil;
import org.linlinjava.jz.hm.exception.BusinessException;
import org.linlinjava.jz.service.HmServantIncomeService;
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.math.BigDecimal;
import java.text.ParseException;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author fuxubing
 */
@Api(value = "保姆-收入信息", description = "保姆-收入信息")
@RestController
@RequestMapping("income")
@Valid
public class HmServantIncomeController {
    @Resource
    private HmEmployRelationService hmEmployRelationService;
    @Resource
    private HmServantIncomeService hmServantIncomeService;
    @Resource
    private HmAppUserService hmAppUserService;
    @Resource
    private ContractSubService contractSubService;
    @Resource
    private ContractSubTemplateService contractSubTemplateService;

    @ApiOperation(value = "总收入", notes = "总收入")
    @GetMapping(value = "getIncome")
    public Object getIncome(@NotNull @RequestParam Long userId) {
        HmAppUser hmAppUser = hmAppUserService.queryById(userId);
        if (null == hmAppUser) {
            return HmResponseUtil.fail_null();
        }

        List<HmEmployRelation> hmEmployRelations = hmEmployRelationService.queryByUserServantId(userId);
        Map<String, Object> map = new HashMap<>(16);
        AtomicReference<BigDecimal> currentMonthlyIncome = new AtomicReference<>(BigDecimal.ZERO);
        AtomicReference<BigDecimal> totalIncome = new AtomicReference<>(BigDecimal.ZERO);
        hmEmployRelations.forEach(item -> {

            LocalDate firstDayOfMonth = DateTimeUtil.firstDayOfMonth(LocalDate.now());
            LocalDate lastDayOfMonth = DateTimeUtil.lastDayOfMonth(LocalDate.now());
            //获取雇佣合同模板信息
            ContractSubTemplate contractSubTemplate = contractSubTemplateService.selectByOrderNo(item.getOrderNo());
            if (null == contractSubTemplate) {
                throw new BusinessException("业务数据异常");
            }
            try {
                currentMonthlyIncome.set(hmServantIncomeService.getTotalIncome(item.getId(), contractSubTemplate, firstDayOfMonth, lastDayOfMonth).add(currentMonthlyIncome.get()));
                totalIncome.set(hmServantIncomeService.getTotalIncome(item.getId(), contractSubTemplate, null, null).add(totalIncome.get()));
            } catch (ParseException e) {
                e.printStackTrace();
            }

        });

        map.put("currentMonthlyIncome", currentMonthlyIncome);
        map.put("totalIncome", totalIncome);

        return HmResponseUtil.ok(map);
    }

    @ApiOperation(value = "历史收入列表", notes = "历史收入列表")
    @GetMapping(value = "getHistoryIncome")
    public Object getHistoryIncome(@NotNull @RequestParam Long userId) {

        HmAppUser hmAppUser = hmAppUserService.queryById(userId);
        if (null == hmAppUser) {
            return HmResponseUtil.fail_null();
        }

        List<HmEmployRelation> hmEmployRelations = hmEmployRelationService.queryByUserServantId(userId);

        List<Map<String, Object>> mapList = Lists.newArrayList();
        hmEmployRelations.forEach(item -> {

            //获取雇佣合同模板信息
            ContractSubTemplate contractSubTemplate = contractSubTemplateService.selectByOrderNo(item.getOrderNo());
            if (null == contractSubTemplate) {
                throw new BusinessException("业务数据异常");
            }
            try {
                Map<String, Object> map = new HashMap<>(16);
                map.put("restDay", item.getRestDay());
                ContractSub contractSub = contractSubService.queryByOrderNo(item.getOrderNo());
                if (null != contractSub) {
                    map.put("serviceModel", contractSub.getServiceModel());
                    map.put("contractEndDate", contractSub.getContractEndDate());
                    map.put("contractState", contractSub.getContractState());
                }

                map.put("employerName", item.getEmployerName());
                map.put("employRelationId", item.getId());

                LocalDate firstDayOfMonth = DateTimeUtil.firstDayOfMonth(LocalDate.now());
                LocalDate lastDayOfMonth = DateTimeUtil.lastDayOfMonth(LocalDate.now());
                BigDecimal currentMonthlyIncome = hmServantIncomeService.getTotalIncome(item.getId(), contractSubTemplate, firstDayOfMonth, lastDayOfMonth);
                BigDecimal totalIncome = hmServantIncomeService.getTotalIncome(item.getId(), contractSubTemplate, null, null);
                map.put("currentMonthlyIncome", currentMonthlyIncome);
                map.put("totalIncome", totalIncome);
                List<Map<String, Object>> historyIncome = hmServantIncomeService.getHistoryIncome(item.getId(), contractSubTemplate);
                map.put("historyIncome", historyIncome);
                mapList.add(map);

            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return HmResponseUtil.ok(mapList);
    }
}
