package com.weather.controller;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.weather.entity.AgriculturalKnowledgePopularization;
import com.weather.service.IAgriculturalKnowledgePopularizationService;
import com.weather.vo.ApiResult;
import com.weather.vo.req.PageProductReq;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 系统公告 前端控制器
 * </p>
 *
 * @author : fengyuhao
 * @since 2021-03-28
 */

@RestController
@RequestMapping("/agricultural/popularization")
public class AgriculturalKnowledgePopularizationController {

    @Autowired
    IAgriculturalKnowledgePopularizationService popularizationService;


    @PostMapping("page")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "农气知识列表", response = AgriculturalKnowledgePopularization.class)
    public ApiResult list(@RequestBody PageProductReq req) throws ParseException, UnsupportedEncodingException {

        IPage<AgriculturalKnowledgePopularization> page = new Page<>();
        page.setSize(req.getLimit());
        page.setCurrent(req.getPage());

        QueryWrapper<AgriculturalKnowledgePopularization> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .orderByDesc(AgriculturalKnowledgePopularization::getEssayType);

        page = popularizationService.page(page, wrapper);
        List<AgriculturalKnowledgePopularization> records = page.getRecords();
        if (records==null||records.size()<=0){
            return ApiResult.noData();
        }


        List<AgriculturalKnowledgePopularization> resList=new ArrayList<>();
        AgriculturalKnowledgePopularization model;

        for (AgriculturalKnowledgePopularization record : records) {
            model=new AgriculturalKnowledgePopularization();
            BeanUtils.copyProperties(record,model);
            // 其他字段处理
            model.setCreateTime(DateUtil.format(model.getInTime(),"yyyy-MM-dd HH:mm:ss"));

            if (model.getContent()!=null) {
                byte[] content = model.getContent();
                String str = new String(content, "GBK");
                model.setDetail(str);
                // 置空
                model.setContent(new byte[1]);
            }else{
                model.setDetail("暂无内容");
            }
            resList.add(model);
        }
        return ApiResult.success(resList);
    }

}
