package com.pj.project.tb_sort_group;

import java.util.List;

import cn.hutool.json.JSONUtil;
import com.pj.project.tb_business_sort.TbBusinessSort;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.pj.utils.sg.*;
import com.pj.project4sp.SP;

import com.pj.current.satoken.StpUserUtil;
import cn.dev33.satoken.annotation.SaCheckPermission;


/**
 * Controller: tb_sort_group -- 业务限制组
 *
 * @author qzy
 */
@RestController
@RequestMapping("/TbSortGroup/")
public class TbSortGroupController {

    /**
     * 底层 Service 对象
     */
    @Autowired
    TbSortGroupService tbSortGroupService;

    /**
     * 增
     */
    @RequestMapping("add")
    @SaCheckPermission(TbSortGroup.PERMISSION_CODE_ADD)
    @Transactional(rollbackFor = Exception.class)
    public AjaxJson add(TbSortGroup t) {
        SoMap so = SoMap.getRequestSoMap();
        String items = so.getString("items");
        List<TbBusinessSort> sorts = JSONUtil.toList(items, TbBusinessSort.class);
        t.setSorts(sorts );
        tbSortGroupService.add(t);
        return AjaxJson.getSuccessData(t);
    }

    /**
     * 删
     */
    @RequestMapping("delete")
    @SaCheckPermission(TbSortGroup.PERMISSION_CODE_DEL)
    public AjaxJson delete(Long id) {
        tbSortGroupService.delete(id);
        return AjaxJson.getSuccess();
    }


    /**
     * 改
     */
    @RequestMapping("update")
    @SaCheckPermission(TbSortGroup.PERMISSION_CODE_EDIT)
    public AjaxJson update(TbSortGroup t) {
        SoMap so = SoMap.getRequestSoMap();
        String items = so.getString("items");
        List<TbBusinessSort> sorts = JSONUtil.toList(items, TbBusinessSort.class);
        t.setSorts(sorts );
        tbSortGroupService.update(t);
        return AjaxJson.getSuccess();
    }

    /**
     * 查 - 根据id
     */
    @RequestMapping("getById")
    public AjaxJson getById(Long id) {
        TbSortGroup t = tbSortGroupService.getById(id);
        return AjaxJson.getSuccessData(t);
    }

    /**
     * 查集合 - 根据条件（参数为空时代表忽略指定条件）
     */
    @RequestMapping("getList")
    public AjaxJson getList() {
        SoMap so = SoMap.getRequestSoMap();
        List<TbSortGroup> list = tbSortGroupService.getList(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);
    }


    /**
     * 改 - 是否启用(0=否,1=是)
     */
    @RequestMapping("updateStatus")
    @SaCheckPermission(TbSortGroup.PERMISSION_CODE_EDIT)
    public AjaxJson updateStatus(Long id, Integer value) {
        int line = SP.publicMapper.updateColumnById(TbSortGroup.TABLE_NAME, "status", value, id);
        return AjaxJson.getByLine(line);
    }
    /**
     * 改 - 是否启用(0=否,1=是)
     */
    @RequestMapping("updateCompletePay")
    @SaCheckPermission(TbSortGroup.PERMISSION_CODE_EDIT)
    public AjaxJson updateCompletePay(Long id, Integer value) {
        int line = SP.publicMapper.updateColumnById(TbSortGroup.TABLE_NAME, "complete_pay", value, id);
        return AjaxJson.getByLine(line);
    }


    // ------------------------- 前端接口 -------------------------


}
