package com.crm.manager.controller;

import com.crm.manager.service.ib.AgentBalanceRecordService;
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.ib.balance.AgentBalanceRecordDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.ib.balance.AgentBalanceRecordAddEntity;
import com.crm.rely.backend.core.entity.ib.balance.AgentBalanceRecordListEntity;
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;

/**
 * Created by max on 2020/7/14.
 */
@RestController
@RequestMapping("/agent/balance")
public class AgentBalanceRecordController {

    @Autowired
    private AgentBalanceRecordService agentBalanceRecordService;

    /**
     * 新增余额修改记录
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto<Object> add(@RequestBody AgentBalanceRecordAddEntity entity, InfoEntity infoEntity) throws Exception {

//        agentBalanceRecordService.add(entity);

        return agentBalanceRecordService.add(entity);
    }

    /**
     * 根据ID获取信息
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/single")
    public BaseResultDto<AgentBalanceRecordDto> searchSingle(@RequestBody SingleLongEntity entity) throws Exception {
        return agentBalanceRecordService.getById(entity.getId());
    }

    /**
     * 获取列表
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/list")
    public BaseResultWithPagerDto<List<AgentBalanceRecordDto>> searchList(@RequestBody AgentBalanceRecordListEntity entity, InfoEntity infoEntity) 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));
        }
        entity.setLang(infoEntity.getLang());
        return agentBalanceRecordService.getList(entity);
    }

    /**
     * 根据ID删除信息
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/delete/single")
    public BaseResultDto<Object> deleteSingle(@RequestBody SingleLongEntity entity) throws Exception {
        return agentBalanceRecordService.deleteById(entity.getId());
    }


}
