package com.crm.manager.controller;

import com.crm.manager.service.CustomGroupService;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.BaseResultWithPagerDto;
import com.crm.rely.backend.core.dto.custom.group.CustomGroupDto;
import com.crm.rely.backend.core.dto.custom.group.CustomGroupStaticDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.group.CustomGroupAddEntity;
import com.crm.rely.backend.core.entity.custom.group.CustomGroupListEntity;
import com.crm.rely.backend.core.entity.custom.group.CustomGroupPlatformListEntity;
import com.crm.rely.backend.core.entity.custom.group.CustomGroupUpdateEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.util.DateUtil;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
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.Date;
import java.util.List;

/**
 * Created by max on 2020/7/8.
 */
@RestController
@RequestMapping("/custom/group")
public class CustomGroupController {

    @Autowired
    private CustomGroupService customGroupService;

    @PostMapping("/add")
    public BaseResultDto<Object> add(@RequestBody CustomGroupAddEntity entity, InfoEntity infoEntity) throws Exception {
        entity.setCode(entity.getCode().trim());
        customGroupService.add(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/update")
    public BaseResultDto<Object> update(@RequestBody CustomGroupUpdateEntity entity, InfoEntity infoEntity) throws Exception {
        customGroupService.updateById(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/delete")
    public BaseResultDto<Object> delete(@RequestBody SingleLongEntity entity) throws Exception {
        customGroupService.deleteById(entity.getId());
        return BaseResultDto.success();
    }

    @PostMapping("/search/single")
    public BaseResultDto<CustomGroupDto> searchSingle(@RequestBody SingleLongEntity entity) throws Exception {

        return BaseResultDto.success(customGroupService.getById(entity.getId()));
    }

    @PostMapping("/search/list")
    public BaseResultWithPagerDto<List<CustomGroupDto>> searchList(@RequestBody CustomGroupListEntity entity) throws Exception {
        if (entity.getStartDate() != null) {
            entity.setStartDate(DateUtil.parseDate(entity.getStartDate()));
        }
        if (!Strings.isNullOrEmpty(entity.getCode())) {
            entity.setCode(entity.getCode().replace("\\", "\\\\"));
        }
        if (entity.getEndDate() != null) {
            Date endDate = DateUtil.operationDay(entity.getEndDate(), 1);
            entity.setEndDate(DateUtil.parseDate(endDate));
        }
        return customGroupService.getList(entity);
    }

    @PostMapping("/list")
    public BaseResultDto<List<String>> list(@RequestBody CustomGroupPlatformListEntity customGroupPlatformListEntity) throws Exception {

        return customGroupService.getList(customGroupPlatformListEntity);
    }

    @PostMapping("/search/info")
    public CustomGroupStaticDto info() throws Exception {
        return customGroupService.getStaticData();
    }

}
