package com.crm.manager.controller;

import com.crm.manager.service.SysSymbolService;
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.system.symbol.SysSymbolDto;
import com.crm.rely.backend.core.dto.system.symbol.SysSymbolStaticDto;
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.system.symbol.SysSymbolAddEntity;
import com.crm.rely.backend.core.entity.system.symbol.SysSymbolListEntity;
import com.crm.rely.backend.core.entity.system.symbol.SysSymbolUpdateEntity;
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/8.
 */
@RestController
@RequestMapping("/symbol")
public class SysSymbolController {

    @Autowired
    private SysSymbolService sysSymbolService;

    /**
     * 添加品种
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto<Object> add(@RequestBody SysSymbolAddEntity entity, InfoEntity infoEntity) throws Exception {

        sysSymbolService.add(entity);
        return BaseResultDto.success();
    }

    /**
     * 更新品种信息
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public BaseResultDto<Object> update(@RequestBody SysSymbolUpdateEntity entity, InfoEntity infoEntity) throws Exception {
        sysSymbolService.updateById(entity);
        return BaseResultDto.success();
    }

    /**
     * 删除品种信息
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public BaseResultDto<Object> delete(@RequestBody SingleLongEntity entity, InfoEntity infoEntity) throws Exception {
        sysSymbolService.deleteById(entity.getId());
        return BaseResultDto.success();
    }

    /**
     * 品种列表
     *
     * @param entity
     * @return
     */
    @PostMapping("/search/list")
    public BaseResultWithPagerDto<List<SysSymbolDto>> searchList(@RequestBody SysSymbolListEntity 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 sysSymbolService.getList(entity);
    }

    /**
     * 单个品种信息
     *
     * @param entity
     * @return
     */
    @PostMapping("/search/single")
    public BaseResultDto<SysSymbolDto> searchSingle(@RequestBody SingleLongEntity entity) throws Exception {
        return BaseResultDto.success(sysSymbolService.getById(entity.getId()));
    }

    /**
     * 获取新增，编辑，查询所需的基础数据
     * @return
     * @throws Exception
     */
    @PostMapping("/search/info")
    public BaseResultDto<SysSymbolStaticDto> getStaticData() throws Exception{

        return BaseResultDto.success(sysSymbolService.getSymbolStaticData());
    }

}
