package com.crm.manager.controller;

import com.crm.manager.service.ControlPanelService;
import com.crm.manager.service.CustomNewsService;
import com.crm.rely.backend.core.constant.PrefixEnum;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.control.panel.statistics.*;
import com.crm.rely.backend.core.entity.control.panel.statistics.ControlPanelStatisticsEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.custom.news.CustomNewsSearchEntity;
import com.crm.rely.backend.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/custom/news")
public class CustomNewsController {

    @Autowired
    private CustomNewsService customNewsService;

    /**
     * 客户动态
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/list")
    public BaseResultDto customDynamic(@RequestBody @Validated CustomNewsSearchEntity entity, InfoEntity infoEntity) throws Exception {
//        if (!PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
//            entity.setStamp(infoEntity.getIbInfo().getStamp());
//        }else {
//            entity.setStamp(null);
//        }
        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getIbInfo().getStamp());
        } else {
            entity.setStamp(null);
        }
        if (entity.getStartDate() != null) {
            entity.setStartDate(DateUtil.parseDate(entity.getStartDate()));
        }
        if (entity.getEndDate() != null) {
            Date endDate = DateUtil.operationDay(entity.getEndDate(), 1);
            entity.setEndDate(DateUtil.parseDate(endDate));
        }
        long dId = infoEntity.getIbInfo().getDepartmentId() == null ? -1 : infoEntity.getIbInfo().getDepartmentId().longValue();
        List<Integer> types = new ArrayList<>(7);
        switch ((int) dId) {
            case 1:
            case 4:
                types.add(1);
                types.add(4);
                types.add(3);
                types.add(5);
                types.add(7);
                types.add(6);
                break;
            case 2:
                types.add(6);
                types.add(3);
                types.add(4);
                break;
            case 3:
                types.add(7);
                break;
            case 5:
                types = null;
                break;
            default:
                if (PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
                    types = null;
                } else {
                    types.add(-1);
                }
                break;
        }
        entity.setTypes(types);
        return customNewsService.searchPageList(entity);
    }


}
