package com.crm.manager.controller;

import com.crm.manager.service.DataRoleService;
import com.crm.manager.service.custom.CustomLoginDemoFeignService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.PrefixEnum;
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.account.CustomAccountDemoDto;
import com.crm.rely.backend.core.entity.custom.info.IbInfoEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.custom.login.demo.CustomLoginDemoSearchListByCustomEntity;
import com.crm.rely.backend.core.entity.custom.login.demo.CustomLoginDemoSearchListEntity;
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.Date;
import java.util.List;

/**
 * @description: 客户demo交易账户控制器
 * @author: houn
 * @create: 2020-07-31 23:02
 **/
@RestController
@RequestMapping("/custom/login/demo")
public class CustomLoginDemoController {


    @Autowired
    private CustomLoginDemoFeignService customLoginDemoFeignService;

    @Autowired
    private DataRoleService dataRoleService;

    /**
     * 查看单个客户列表(customId不为空)
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/custom/list")
    public BaseResultDto<List<CustomAccountDemoDto>> searcherList(@RequestBody @Validated CustomLoginDemoSearchListByCustomEntity 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.getCustomId() == null) {
            return BaseResultDto.error(Constants.PARAMETER_ERROR);
        }
        IbInfoEntity ibInfoEntity = infoEntity.getIbInfo();
        if (!PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
            entity.setStamp(ibInfoEntity.getStamp());
        }
        entity.setGroupId(ibInfoEntity.getGroupId());
        return customLoginDemoFeignService.searchCustomList(entity);
    }

    /**
     * 查看客户交易账户
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/list")
    public BaseResultWithPagerDto<List<CustomAccountDemoDto>> searcherCustomList(@RequestBody @Validated CustomLoginDemoSearchListEntity 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));
        }
        return customLoginDemoFeignService.searchList(entity);
    }
}
