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.StaOutPay;
import com.liang.entity.StaTravelexpenses;
import com.liang.service.StaOutPayService;
import com.liang.service.StaStatisticalSummaryService;
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.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 外委合同支付(staOutPayController)表控制层
 */
@Controller
@RequestMapping("staOutPay")
public class staOutPayController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private StaOutPayService staOutPayService;

    @Resource
    private StaStatisticalSummaryService staStatisticalSummaryService;

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


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

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.staOutPayService.deleteById(id));
    }

    /**
     * 初始化外委合同支付列表
     *
     * @param zb
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list/{zb}", method = RequestMethod.POST)
    public BaseResult list(@PathVariable Integer zb) {
        List<StaOutPay> list = staOutPayService.getWwhtList(zb);

        PageInfo<StaOutPay> pageinfo = new PageInfo<>(list);
        //取出查询结果
        List<StaOutPay> rows = pageinfo.getList();
        int total = (int) pageinfo.getTotal();
        Map<String, Object> result = new HashMap<>();
        result.put(RESULT_ROWS, rows);
        result.put(RESULT_TOTAL, total);

        return BaseResult.success(result);
    }

    /**
     * 新建数据
     *
     * @return
     */
    @RequestMapping(value = "/forAdd", method = RequestMethod.GET)
    public String forAdd(Model model) {
        return "staoutpay/add";
    }

    /**
     * 批量保存
     *
     * @param data
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/batchInsert/{zb}", method = RequestMethod.POST)
    public BaseResult batchInsert(@RequestBody String data, @PathVariable Integer zb) {
        //删除外委合同支付
        staOutPayService.deleteByZb(zb);

        List<StaOutPay> staOutPayList = JsonTool.parseArray(data, StaOutPay.class);
        for (int i = 0; i < staOutPayList.size(); i++) {
            staOutPayList.get(i).setZb(zb);
        }
        if (staOutPayList.size() > 0) {
            int num = staOutPayService.insertBatch(staOutPayList);
            if (num == staOutPayList.size()) {
                return BaseResult.success("外委合同支付费保存成功！");
            } else {
                return BaseResult.failure("外委合同支付费保存失败！");
            }
        } else {
            return BaseResult.success("没有要保存的信息");
        }
    }

    /**
     * 编辑数据
     *
     * @return
     */
    @RequestMapping(value = "/forUpdate", method = RequestMethod.GET)
    public String forUpdate() {
        return "staoutpay/update";
    }

    /**
     * 查看数据
     *
     * @return
     */
    @RequestMapping(value = "/forView", method = RequestMethod.GET)
    public String forView() {
        return "staoutpay/view";
    }

    /**
     * 统计报表外委费页面查询
     *
     * @param model
     * @return
     */
    @RequestMapping("/search")
    public String search(Model model) {
        List<HashMap<String, String>> lists = staOutPayService.getAllOutPay();
        model.addAttribute("outPay", lists);
        return "statistical/outPay";
    }

}
