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.common.utils.StringUtils;
import com.bootdo.datas.dto.GyDataImportDTO;
import com.bootdo.datas.service.GyDataService;
import com.google.common.collect.Lists;
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.web.bind.annotation.*;

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

/**
 * 回收数据控制层
 *
 * @author Mr.xiBel
 */

@Controller
@RequestMapping("/datas/dataRecover")
public class DataRecoverController {

    private static final Logger log = LoggerFactory.getLogger(DataRecoverController.class);

    @Autowired
    private GyDataService gyDataService;

    @GetMapping()
    @Log("访问数据回收")
    @RequiresPermissions("datas:dataRecover:data")
    String Data() {

        return "datas/dataRecover/data";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("datas:dataRecover:data")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        String dataStatus = (String) params.get("dataStatus");
        if(StringUtils.isNotEmpty(dataStatus)) {
            query.put("dataStatusArrs", Lists.newArrayList((dataStatus)));
        }
        List<GyDataImportDTO> dataList = gyDataService.listRecover(query);
        int total = gyDataService.countRecover(query);
        return new PageUtils(dataList, total);
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("datas:dataRecover:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        GyDataImportDTO data = gyDataService.get(id);
        model.addAttribute("data", data);
        return "datas/dataRecover/edit";
    }

    /**
     * 恢复
     */
    @PostMapping("/recover")
    @ResponseBody
    @RequiresPermissions("datas:dataRecover:recover")
    public R recover(Long id) {
        if (gyDataService.recover(id) > 0) {
            return R.ok();
        }
        return R.error("不能恢复，同企业下已经存在同名的数据（也可能是存在同名驳回数据）");
    }

    /**
     * 物理删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("datas:dataRecover:remove")
    public R batchPhysicalDeletion(Long id) {
        if (gyDataService.batchPhysicalDeletion(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

}
