package com.liang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liang.common.base.BaseResult;
import com.liang.common.annotation.LogAround;
import com.liang.common.base.BaseController;
import com.liang.entity.BaseNation;
import com.liang.service.BaseNationService;
import io.swagger.annotations.ApiOperation;
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.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (BaseNation)表控制层
 *
 * @author makejava
 * @since 2023-02-15 23:13:15
 */
@Controller
@RequestMapping("baseNation")
public class BaseNationController extends BaseController {
    final static Logger logger = LoggerFactory.getLogger(com.liang.controller.BaseNationController.class);
    /**
     * 服务对象
     */
    @Resource
    private BaseNationService baseNationService;

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

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

    /**
     * 新增数据
     *
     * @param baseNation 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<BaseNation> add(BaseNation baseNation) {
        return ResponseEntity.ok(this.baseNationService.insert(baseNation));
    }

    /**
     * 编辑数据
     *
     * @param baseNation 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<BaseNation> edit(BaseNation baseNation) {
        return ResponseEntity.ok(this.baseNationService.update(baseNation));
    }

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

    @ApiOperation(value = "页面初始化", notes = "页面初始化")
    @RequestMapping(value = "/init", method = RequestMethod.GET)
    public String init() {
        return "basenation/list";
    }

    @LogAround("数据分页查询")
    @ApiOperation(value = "数据分页查询", notes = "数据分页查询")
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public BaseResult list(@RequestParam("page") Integer pageNum,
                           @RequestParam("limit") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        List<BaseNation> list = baseNationService.queryAll();

        PageInfo<BaseNation> pageinfo = new PageInfo<>(list);
        // 取出查询结果
        List<BaseNation> 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);
    }

    @RequestMapping(value = "/mzlist", method = RequestMethod.GET)
    public Object mzlist(@RequestParam("page") Integer pageNum,
                         @RequestParam("limit") Integer pageSize,
                         @RequestParam(required = false, defaultValue = "", value = "mzmc") String mzmc) {
        PageHelper.startPage(pageNum, pageSize);

        Map<String, String> paraMap = new HashMap<>();
        paraMap.put("mzmc", mzmc);
        List<BaseNation> list = baseNationService.getNationList(paraMap);

        PageInfo<BaseNation> pageinfo = new PageInfo<>(list);
        // 取出查询结果
        List<BaseNation> rows = pageinfo.getList();
        int total = (int) pageinfo.getTotal();

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("msg", "");
        resultMap.put("count", total);
        resultMap.put("data", list);

        return resultMap;
    }
}

