package com.nantian.cloud.jljc.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nantian.cloud.jljc.entity.HKind;
import com.nantian.cloud.jljc.entity.HTableHead;
import com.nantian.cloud.jljc.entity.HTableKind;
import com.nantian.cloud.jljc.service.HKindService;
import com.nantian.cloud.jljc.service.HTableHeadService;
import com.nantian.cloud.jljc.service.HTableKindService;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


/**
 * （慧能）能源表
 * @since 2020-11-11
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/hKind")
@Api(value = "hKind", tags = "（慧能）行业、产品、工序")
public class HKindController {

    private final HKindService hKindService;
    private final HTableHeadService hTableHeadService;
    private final HTableKindService hTableKindService;

    /**
     * 查询全部（慧能）行业、产品、工序
     */
    @GetMapping("/list")
    @SysLog("查询全部（慧能）行业、产品、工序")
    @ApiOperation(value = "查询全部（慧能）行业、产品、工序", notes = "查询全部（慧能）行业、产品、工序")
    public R selectAll(HKind hKind) {
        return R.ok(hKindService.list(Wrappers.query(hKind)));
    }


    /**
     * 查询全部（慧能）行业、产品、工序
     */
    @GetMapping("/kindByCode")
    @SysLog("查询全部（慧能）行业、产品、工序")
    @ApiOperation(value = "查询全部（慧能）行业、产品、工序", notes = "查询全部（慧能）行业、产品、工序")
    public R listByCode(HKind hKind) {
        List<HKind> list = hKindService.list(Wrappers.query(hKind));
        List<HKind> newList = new ArrayList<HKind>();
        for (HKind kind : list) {
            if (kind.getCode()!=null) {
                List<HTableHead> hTableHeads = hTableHeadService.getTableHeadByCodes(kind.getCode());
                if(hTableHeads.size()>0){
                    /*HTableHead tableHead = hTableHeads.get(0);
                    List<String> hTableHeadList = new ArrayList<>();
                    hTableHeadList.add("code,"+tableHead.getCode());
                    hTableHeadList.add("productName,"+tableHead.getProductName());
                    hTableHeadList.add("production,"+tableHead.getProduction());
                    hTableHeadList.add("unit,"+tableHead.getUnit());
                    hTableHeadList.add("calculationResults,"+tableHead.getCalculationResults());
                    hTableHeadList.add("refModulus,"+tableHead.getRefModulus());
                    kind.setHTableHeadList(hTableHeadList);*/
                    kind.setHTableHeadList(hTableHeads);
                }

                List<HTableKind> hTableKinds = hTableKindService.getTableKindByCodes(kind.getCode());
                if(hTableKinds.size()>0){
                    kind.setHTableKindList(hTableKinds);
                }
            }
            newList.add(kind);
        }
        return R.ok(newList);
    }


    /**
     * 获取树形结构
     */
    @GetMapping("/tree")
    @ApiOperation(value = "获取树形结构", notes = "获取树形结构")
    @ApiImplicitParam(name = "hKind", value = "行业、产品、工序对象", required = false, paramType = "path")
    public R getTree(HKind hKind) {
        return R.ok(hKindService.getTree(hKind));
    }

    /**
     * 获取树形结构通过叶子获取树形结构
     */
    @GetMapping("/treeByLeaf")
    @ApiOperation(value = "获取树形结构通过叶子获取树形结构", notes = "获取树形结构通过叶子获取树形结构")
    @ApiImplicitParam(name = "hKind", value = "行业、产品、工序对象", required = false, paramType = "path")
    public R treeByLeaf(String code) {
        return R.ok(hKindService.treeByLeaf(code));
    }


}
