package com.chinaweal.aiccs.aiccs.revoke.controller;


import com.chinaweal.aiccs.aiccs.revoke.entity.TsCase;
import com.chinaweal.aiccs.aiccs.revoke.entity.Clue;
import com.chinaweal.aiccs.aiccs.revoke.entity.Tsrevtasklist;
import com.chinaweal.aiccs.aiccs.revoke.service.CaseService;
import com.chinaweal.aiccs.aiccs.revoke.service.ClueService;
import com.chinaweal.aiccs.aiccs.revoke.service.TsrevtasklistService;
import com.chinaweal.aiccs.common.base.controller.BaseController;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import com.chinaweal.youfool.framework.springboot.rest.ResultCode;
import com.chinaweal.youfool.framework.springboot.util.AssertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


/**
 * <p>
 * 线索信息 前端控制器
 * </p>
 *
 * @author Lee
 * @since 2021-03-26
 */
@RestController
@RequestMapping("/clue")
@Transactional
public class ClueController extends BaseController {

    @Autowired
    private ClueService clueService;
    @Autowired
    private TsrevtasklistService tsrevtasklistService;
    @Autowired
    private CaseService caseService;

    /**
     * 线索录入保存
     * @param clueModel 线索信息表
     * @param tasklistid 待办任务id
     * @param request
     * @return
     */
    @PostMapping("saveTaskClue")
    public RestResult<?> saveTaskClue(Clue clueModel, String tasklistid, HttpServletRequest request){

        AssertUtils.isNotBlank(tasklistid);
        AICUser aicUser = getLoginUser(request);
        boolean saveOrUpdate;
        //还没创建线索表
        if(clueModel != null && StringUtils.isBlank(clueModel.getClueId())){
            saveOrUpdate  = clueService.saveOrUpdate(clueModel);
            if(saveOrUpdate){
                Tsrevtasklist tsrevtasklist = tsrevtasklistService.getById(tasklistid);
//                tsrevtasklist.setClueid(clueModel.getClueId());
                tsrevtasklistService.updateById(tsrevtasklist);

                //生成案件
                TsCase tsCaseModel = new TsCase();
                tsCaseModel.setBizseq(tsrevtasklist.getBizseqid());
                tsCaseModel.setCaseFrom(clueModel.getClueFrom());
//                tsCaseModel.setCaseNo("GZ123456789");
                tsCaseModel.setCaseName(clueModel.getClueName());
//                tsCaseModel.setRegistTime(clueModel.getAcceptTime());

                return RestResult.ok(caseService.save(tsCaseModel));
            }
        }else {
            saveOrUpdate  = clueService.saveOrUpdate(clueModel);
        }
        return RestResult.ok(saveOrUpdate);
    }

    /**
     * 通过待办任务id获取当前线索信息
     * @param tasklistid 待办任务id
     * @return
     */
    @GetMapping("getClueByTaskListid")
    public RestResult<?> getClueByTasklistid(String tasklistid){
        AssertUtils.isNotBlank(tasklistid);
        Tsrevtasklist tsrevtasklist = tsrevtasklistService.getById(tasklistid);
        String clueid = tsrevtasklist.getCaseid();
        if(StringUtils.isNotBlank(clueid)){
           return RestResult.ok(clueService.getById(clueid));
        }
        return RestResult.error(ResultCode.DATA_NONE);
    }
}

