package com.crm.manager.controller;

import com.alibaba.fastjson.JSON;
import com.crm.manager.service.CustomLoginDeleteRecordService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.entity.custom.login.delete.record.CustomLoginDeleteRecordAddEntity;
import com.crm.rely.backend.core.pojo.table.CustomLoginDeleteRecordTable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;
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.List;

@Slf4j
@RestController
@RequestMapping("/custom/login/delete/record")
public class CustomLoginDeleteRecordController {

    @Autowired
    private CustomLoginDeleteRecordService service;


    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated CustomLoginDeleteRecordAddEntity entity) throws Exception {
        service.save(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/adds")
    public BaseResultDto adds(@RequestBody @Validated List<CustomLoginDeleteRecordAddEntity> entities) throws Exception {
        service.save(entities);
        return BaseResultDto.success();
    }

    @JmsListener(destination = Constants.CUSTOM_LOGIN_DELETE_INFO)
    @Transactional(rollbackFor = Exception.class)
    public void add(String content) throws Exception {

        log.info(String.format("CUSTOM_LOGIN_DELETE_INFO," +
                "content:%s", content));
        CustomLoginDeleteRecordAddEntity entity = JSON.parseObject(content,
                CustomLoginDeleteRecordAddEntity.class);

        if (entity == null) {
            log.error("CUSTOM_LOGIN_DELETE_INFO," +
                    "entity is null");
            return;
        }
        service.save(entity);
    }

    @JmsListener(destination = Constants.CUSTOM_LOGIN_DELETE_INFOS)
    @Transactional(rollbackFor = Exception.class)
    public void adds(String content) throws Exception {

        log.info(String.format("CUSTOM_LOGIN_DELETE_INFOS," +
                "content:%s", content));
        List<CustomLoginDeleteRecordAddEntity> entities = JSON.parseArray(content,
                CustomLoginDeleteRecordAddEntity.class);

        if (entities == null || entities.isEmpty()) {
            log.error("CUSTOM_LOGIN_DELETE_INFOS," +
                    "entity is null");
            return;
        }
        service.save(entities);
    }

    @JmsListener(destination = Constants.CUSTOM_LOGIN_MARK_DELETE)
    @Transactional(rollbackFor = Exception.class)
    public void delCustomLogin(String content) throws Exception {

        log.info(String.format("CUSTOM_LOGIN_MARK_DELETE,content:%s", content));
        CustomLoginDeleteRecordTable table = JSON.parseObject(content,
                CustomLoginDeleteRecordTable.class);

        if (table == null) {
            log.error("CUSTOM_LOGIN_MARK_DELETE, entity is null");
            return;
        }
        service.delCustomLogin(table);
    }

    @Transactional(rollbackFor = Exception.class)
    @Scheduled(cron = "0 14 11 * *  ?")
    public void customLoginMarkDeleteTask() throws Exception {
        try {
            log.info("标记需要删除的账户开始 ---------------");
            service.customLoginMarkDeleteTask();
            log.info("标记需要删除的账户结束 ---------------");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }

    }

}
