fangyuan
2022-11-21 5cba031b4fcc437568a46295739fda3dae7ae41f
ycl-platform/src/main/java/com/ycl/controller/NewsInformationController.java
@@ -6,12 +6,22 @@
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ycl.entity.NewsColumnInformation;
import com.ycl.entity.NewsInformation;
import com.ycl.service.NewsInformationService;
import com.ycl.entity.NewsInformationPolice;
import com.ycl.entity.NewsPolice;
import com.ycl.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import sun.nio.ch.IOUtil;
import javax.annotation.Resource;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
/**
@@ -22,6 +32,7 @@
 */
@RestController
@RequestMapping("newsInformation")
@Api(tags = "资讯管理")
public class NewsInformationController extends ApiController {
    /**
     * 服务对象
@@ -29,16 +40,24 @@
    @Resource
    private NewsInformationService newsInformationService;
    @Autowired
    private NewsPoliceService policeService;
    @Autowired
    private NewsInformationPoliceService newsInformationPoliceService;
    @Autowired
    private NewsColumnInformationService newsColumnInformationService;
    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param newsInformation 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<NewsInformation> page, NewsInformation newsInformation) {
        return success(this.newsInformationService.page(page, new QueryWrapper<>(newsInformation)));
    @ApiOperation(value = "查询所有数据")
    public R selectAll() {
        return success(this.newsInformationService.selectAllInformation());
    }
    /**
@@ -48,6 +67,7 @@
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation(value = "按id查询数据")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.newsInformationService.getById(id));
    }
@@ -58,9 +78,30 @@
     * @param newsInformation 实体对象
     * @return 新增结果
     */
    @Transactional(rollbackFor = SQLException.class)
    @PostMapping
    @ApiOperation(value = "新增数据")
    public R insert(@RequestBody NewsInformation newsInformation) {
        return success(this.newsInformationService.save(newsInformation));
        Integer saveResult = this.newsInformationService.insertOneInformation(newsInformation);
        Integer informationId=newsInformation.getId();
        if (newsInformation.getIsSign()==0){
            List<String> departmentIds = newsInformation.getDepartmentId();
            for (String departmentId:departmentIds){
                QueryWrapper<NewsPolice> wrapper = new QueryWrapper();
                wrapper.eq("news_department_id",departmentId);
                List<NewsPolice> list = policeService.list(wrapper);
                if (!list.isEmpty()){
                    for (NewsPolice newsPolice:list){
                        newsInformationPoliceService.save(NewsInformationPolice.builder().newsPoliceId(newsPolice.getId()).newsInformationId(informationId).isSign(0).build());
                    }
                }
            }
        }
        for (String columnId:newsInformation.getColumnId()){
            newsColumnInformationService.save(NewsColumnInformation.builder().columnId(Integer.parseInt(columnId)).informationId(informationId).build());
        }
        return success(saveResult);
    }
    /**
@@ -70,8 +111,29 @@
     * @return 修改结果
     */
    @PutMapping
    @ApiOperation(value = "修改数据")
    @Transactional
    public R update(@RequestBody NewsInformation newsInformation) {
        return success(this.newsInformationService.updateById(newsInformation));
        Integer informationId = newsInformation.getId();
        if (newsInformation.getIsSign()==0){
            newsInformationPoliceService.remove(new QueryWrapper<NewsInformationPolice>().eq("news_information_id",informationId));
            List<String> departmentIds = newsInformation.getDepartmentId();
            for (String departmentId:departmentIds){
                QueryWrapper<NewsPolice> wrapper = new QueryWrapper();
                wrapper.eq("news_department_id",departmentId);
                List<NewsPolice> list = policeService.list(wrapper);
                if (!list.isEmpty()){
                    for (NewsPolice newsPolice:list){
                        newsInformationPoliceService.save(NewsInformationPolice.builder().newsPoliceId(newsPolice.getId()).newsInformationId(informationId).isSign(0).build());
                    }
                }
            }
        }
        newsColumnInformationService.remove(new QueryWrapper<NewsColumnInformation>().eq("information_id",informationId));
        for (String columnId:newsInformation.getColumnId()){
            newsColumnInformationService.save(NewsColumnInformation.builder().columnId(Integer.parseInt(columnId)).informationId(informationId).build());
        }
        return success(this.newsInformationService.updateInformationById(newsInformation));
    }
    /**
@@ -81,6 +143,7 @@
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation(value = "删除数据")
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.newsInformationService.removeByIds(idList));
    }