package com.crm.manager.controller;

import com.crm.manager.service.CustomGroupDemoService;
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.dto.custom.group.demo.CustomGroupDemoDto;
import com.crm.rely.backend.core.dto.custom.group.demo.CustomGroupDemoStaticDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.group.demo.CustomGroupDemoAddEntity;
import com.crm.rely.backend.core.entity.custom.group.demo.CustomGroupDemoListEntity;
import com.crm.rely.backend.core.entity.custom.group.demo.CustomGroupDemoUpdateEntity;
import com.crm.rely.backend.util.DateUtil;
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;


@RestController
@RequestMapping("/custom/group/demo")
public class CustomGroupDemoController {

    @Autowired
    private CustomGroupDemoService customGroupDemoService;

    @PostMapping("/add")
    public BaseResultDto<Object> add(@RequestBody CustomGroupDemoAddEntity entity) throws Exception {
        customGroupDemoService.add(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/update")
    public BaseResultDto<Object> update(@RequestBody CustomGroupDemoUpdateEntity entity) throws Exception {
        customGroupDemoService.updateById(entity);
        return BaseResultDto.success();
    }

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

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

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

    @PostMapping("/search/list")
    public BaseResultWithPagerDto<List<CustomGroupDemoDto>> searchList(@RequestBody CustomGroupDemoListEntity entity) throws Exception {
        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));
        }
        return customGroupDemoService.getList(entity);
    }
    @PostMapping("/search/info")
    public CustomGroupDemoStaticDto info() throws Exception {
        return customGroupDemoService.getStaticData();
    }
}
