package com.liang.controller;

import com.github.pagehelper.PageInfo;
import com.liang.common.JsonTool;
import com.liang.common.base.BaseController;
import com.liang.common.base.BaseResult;
import com.liang.entity.PrjCompletecompany;
import com.liang.entity.PrjSubtopic;
import com.liang.service.PrjCompletecompanyService;
import com.liang.service.PrjSubtopicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * (PrjSubtopic)表控制层
 *
 * @author makejava
 * @since 2023-03-20 16:25:07
 */
@Controller
@RequestMapping("prjSubtopic")
public class PrjSubtopicController extends BaseController {
    final static Logger logger = LoggerFactory.getLogger(com.liang.controller.PrjSubtopicController.class);
    /**
     * 服务对象
     */
    @Resource
    private PrjSubtopicService prjSubtopicService;
    @Resource
    private PrjCompletecompanyService prjCompletecompanyService;

    /**
     * 分页查询
     *
     * @param prjSubtopic 筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<PrjSubtopic>> queryByPage(PrjSubtopic prjSubtopic, PageRequest pageRequest) {
        return ResponseEntity.ok(this.prjSubtopicService.queryByPage(prjSubtopic, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<PrjSubtopic> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.prjSubtopicService.queryById(id));
    }

    /**
     * 删除数据
     *
     * @return 删除是否成功
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public BaseResult delete(@PathVariable Integer id) {
        boolean flag = prjSubtopicService.deleteById(id);
        if (flag) {
            return BaseResult.success("删除成功!");
        } else {
            return BaseResult.failure("删除失败!");
        }
    }

    /**
     * 获取项目预算列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list/{xm}", method = RequestMethod.POST)
    public BaseResult list(@PathVariable Integer xm) {
        List<PrjSubtopic> list = prjSubtopicService.getSubtopicList(xm);
        List<PrjCompletecompany>prjCompletecompanyListFy=prjCompletecompanyService.getCompanyListFy(xm);
        PageInfo<PrjSubtopic> pageinfo = new PageInfo<>(list);
        //取出查询结果
        List<PrjSubtopic> rows = pageinfo.getList();
        int total = (int) pageinfo.getTotal();
        Map<String, Object> result = new HashMap<>();

        Double sum = 0.00;
        for (int i = 0; i < rows.size(); i++) {
            sum = sum + rows.get(i).getJf();
        }

        result.put(RESULT_ROWS, rows);
        result.put(RESULT_TOTAL, total);
        result.put("prjCompletecompanyListFy",prjCompletecompanyListFy);
        result.put("sum", sum);
        System.out.println("sum:");
        System.out.println(sum);
        return BaseResult.success(result);
    }

    /**
     * 新建数据
     *
     * @return
     */
    @RequestMapping(value = "/forAdd/{xm}", method = RequestMethod.GET)
    public String forAdd(Model model, @PathVariable Integer xm) {
        //查询项目的时间信息 暂时只需要给子课题默认完成时间
        List<Map<String, Object>> projectDate = prjSubtopicService.queryProjectDate(xm);
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        Date rq = (Date) projectDate.get(0).get("WC");
        Date qs = (Date) projectDate.get(0).get("QS");

        String wcrq = dateFormat.format(rq);
        String qsrq = dateFormat.format(qs);
        model.addAttribute("qsrq", qsrq);
        model.addAttribute("wcrq", wcrq);
        model.addAttribute("xm", xm);
        return "prjsubtopic/add";
    }

    /**
     * 保存数据（基本信息）
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/doAdd", method = RequestMethod.POST)
    public BaseResult doAdd(PrjSubtopic subtopic) {
        int num = prjSubtopicService.insert(subtopic);
        if (num > 0) {
            return BaseResult.success("子课题基本信息保存成功!", subtopic);
        } else if (num == -1) {
            return BaseResult.failure("该子课题已存在，请选中后编辑！!");
        } else {
            return BaseResult.failure("子课题基本信息保存失败!");
        }
    }

    /**
     * 编辑数据（基本信息）
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/doUpdate", method = RequestMethod.POST)
    public BaseResult doUpdate(PrjSubtopic subtopic) {
        int num = prjSubtopicService.update(subtopic);
        if (num > 0) {
            return BaseResult.success("子课题基本信息保存成功!", subtopic);
        } else {
            return BaseResult.failure("子课题基本信息保存失败!");
        }
    }

    @RequestMapping(value = "/forUpdate/{id}", method = RequestMethod.GET)
    public String forUpdate(Model model, @PathVariable Integer id) {
        PrjSubtopic subtopic = prjSubtopicService.queryById(id);
        List<Map<String, Object>> projectDate = prjSubtopicService.queryProjectDate(subtopic.getXm());
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        Date rq = (Date) projectDate.get(0).get("WC");
        Date qs = (Date) projectDate.get(0).get("QS");
        String wcrq = dateFormat.format(rq);
        String qsrq = dateFormat.format(qs);
        model.addAttribute("qsrq", qsrq);
        model.addAttribute("wcrq", wcrq);
        model.addAttribute("subtopic", subtopic);

        return "prjsubtopic/update";
    }

    @RequestMapping(value = "/forView/{id}", method = RequestMethod.GET)
    public String forView(Model model, @PathVariable Integer id) {
        PrjSubtopic subtopic = prjSubtopicService.queryById(id);
        model.addAttribute("subtopic", subtopic);

        return "prjsubtopic/view";
    }

    /**
     * 批量删除子课题
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    public BaseResult batchDelete(@RequestBody String data) {

        List<Integer> idList = JsonTool.getListID(data);
        int num = prjSubtopicService.batchDelete(idList);
        if (num > 0) {
            return BaseResult.success("批量删除成功!");
        } else {
            return BaseResult.failure("批量删除失败!");
        }
    }
}

