package com.pqs9000.controller.responsibility;

import com.njcn.pojo.commons.HttpResult;
import com.njcn.utils.PubUtils;
import com.pqs9000.pojo.responsibility.UserExcelData;
import com.pqs9000.pojo.responsibility.UserExcelDataNoIntegrity;
import com.pqs9000.service.responsibility.IUserExcelDataNoIntegrityService;
import com.pqs9000.service.responsibility.IUserExcelDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
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.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author hongawen
 * @Date: 2019/4/24 10:11
 */
@Controller
@RequestMapping("responsibility")
public class ResponsibilityUserDataController {


    private static final Logger logger=LoggerFactory.getLogger(ResponsibilityUserDataController.class);

    @Autowired
    private IUserExcelDataService userExcelDataService;

    @Autowired
    private IUserExcelDataNoIntegrityService userExcelDataNoIntegrityService;

    /**
     * 责任划分列表--》用采数据列表
     */
    @GetMapping("userDataList")
    public String userDataList(){
        return "business/responsibility/userDataList";
    }



    /**
     * 用采数据列表--》完整性不足详情列表
     */
    @GetMapping("noIntergrity")
    public ModelAndView noIntergrity(String excelIndex){
        ModelAndView mv=new ModelAndView();
        mv.addObject("excelIndex",excelIndex);
        mv.setViewName("business/responsibility/lackIntergrity");
        return mv;
    }

    /**
     * 根据用采数据索引获取其完整性不足的数据展示
     * @param excelIndex 索引
     */
    @PostMapping("getLackIntergrityList")
    @ResponseBody
    public HttpResult getLackIntergrityList(String excelIndex){
        HttpResult result;
        if(StringUtils.isEmpty(excelIndex)){
            result=PubUtils.assignmentResult(null,500,"参数不合法");
            return result;
        }
        List<UserExcelDataNoIntegrity> userExcelDataNoIntegrities;
        try{
            userExcelDataNoIntegrities=userExcelDataNoIntegrityService.getUserExcelDataNoIntergritys(excelIndex);
            if(CollectionUtils.isEmpty(userExcelDataNoIntegrities)){
                result=PubUtils.assignmentResult(null,200,"完整性详情数据不存在！");
            }else{
                result=PubUtils.assignmentResult(userExcelDataNoIntegrities,200,"获取完整性详情数据成功！");
            }
        }catch (Exception e){
            logger.error("获取完整性详情数据成功异常，异常为："+e.toString());
            result=PubUtils.assignmentResult(null,500,"获取完整性详情数据异常！");
        }
        return result;
    }

    /**
     * 根据索引删除责任记录和文件数据
     * @param userDataIndex 用采数据索引
     */
    @PostMapping("deleteUserData")
    @ResponseBody
    public HttpResult deleteUserData(String userDataIndex) {
        HttpResult result;
        try {
            userExcelDataService.deleteExcelDataByIndex(userDataIndex);
            result=PubUtils.assignmentResult(null,200,"删除记录成功");
        } catch (Exception e) {
            logger.error("根据索引删除责任记录和文件数据异常,异常为：" + e.toString());
            result=PubUtils.assignmentResult(null,500,"删除记录失败");
        }
        return result;
    }



}
