package com.bootdo.datas.controller;


import com.bootdo.common.annotation.Log;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.datas.dto.ExamineLogDTO;
import com.bootdo.datas.dto.GyDataImportDTO;
import com.bootdo.datas.service.ExamineLogService;
import com.bootdo.datas.service.GyDataService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/datas/dataEntry")
public class DataEntryController {
    private static Logger log = LoggerFactory.getLogger(DataEntryController.class);


    @Autowired
    private GyDataService gyDataService;
    @Autowired
    private ExamineLogService examineLogService;

    @GetMapping()
    @Log("访问数据条目")
    @RequiresPermissions("datas:dataEntry:data")
    String Data() {
        return "datas/dataEntry/data";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("datas:dataEntry:data")
    public PageUtils dataEntryList(@RequestParam Map<String, Object> map) {
        // 查询列表数据
        Query query = new Query(map);
        query.put("dataStatusArrs", Lists.newArrayList(0,1,2,3,4));

        List<GyDataImportDTO> dataList = gyDataService.dataEntryList(query);
        int total = gyDataService.dataEntryCountTotal(query);
        return new PageUtils(dataList, total);
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("datas:dataEntry:remove")
    public R remove(Long id) {
        //判断一下该条数据是否被风评系统使用
        int cnt = gyDataService.countAaAssData(id);
        if(cnt > 0){
            return R.error("该条数据已经被风评系统使用，不能删除！");
        }
        Long[] longArray = new Long[]{id};
        gyDataService.batchRemove(longArray);
        return R.ok();
    }


    @GetMapping("/edit/{id}")
    @RequiresPermissions("datas:dataEntry:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        log.info("edit id:{}", id);
        GyDataImportDTO data = gyDataService.get(id);
        Map<String, Object> reqParamMap = Maps.newHashMap();
        reqParamMap.put("dataType", "DI_OPER");
        reqParamMap.put("uniqueKey", id);
        log.info("日志查询开始");
        List<ExamineLogDTO> examineLogList = examineLogService.list(reqParamMap);
        log.info("日志查询结束"+examineLogList.size());
        model.addAttribute("data", data);
        if (!CollectionUtils.isEmpty(examineLogList)) {
            log.info("examineLog-----"+examineLogList);
            model.addAttribute("examineLog", examineLogList);
        }
        return "datas/data/edit";
    }
}
