package com.huiju.foyo.controller;

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.huiju.foyo.common.BaseController;
import com.huiju.foyo.common.ConstantFoyo;
import com.huiju.foyo.common.ResponseBean;
import com.huiju.foyo.model.TaService;
import com.huiju.foyo.model.TaServiceLabel;
import com.huiju.foyo.service.ITaServiceLabelService;
import com.huiju.foyo.service.ITaServiceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
    * 服务表  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-08-21
 */
@RestController
@RequestMapping("/")
public class TaServiceController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(TaServiceController.class);

    @Autowired
    public ITaServiceService iTaServiceService;

    @Autowired
    private ITaServiceLabelService taServiceLabelService;

    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/service/list",method= RequestMethod.GET)
    public ResponseBean taServiceList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                      @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<TaService> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaService> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status",ConstantFoyo.STATUS_EFFECTIVE);
            queryWrapper.orderByDesc("sort");
            queryWrapper.orderByDesc("create_time");

            IPage<TaService> result = iTaServiceService.page(pg, queryWrapper);
            responseBean.addSuccess(result);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taServiceList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 保存对象
     * @param taService 实体对象
     * @return
     */
    @RequestMapping(value="/admin/service/add",method= RequestMethod.POST)
    public ResponseBean taServiceAdd(@RequestBody TaService taService){
        ResponseBean responseBean = new ResponseBean();
        try {
            taService.setStatus(ConstantFoyo.STATUS_EFFECTIVE);
            taService.setCreateTime(LocalDateTime.now());
            taService.setCreateBy(ConstantFoyo.USER_ADMIN);
            if (iTaServiceService.save(taService)){
                //保存对应的标签
                List<TaServiceLabel> serviceLabelList = new ArrayList<>();
                List<String> labelList = taService.getLabelList();
                labelList.stream().forEach(e -> {
                    TaServiceLabel taServiceLabel = new TaServiceLabel();
                    taServiceLabel.setLabelName(e);
                    taServiceLabel.setServiceId(taService.getId());
                    taServiceLabel.setCreateTime(LocalDateTime.now());
                    taServiceLabel.setCreateBy(ConstantFoyo.USER_ADMIN);
                    serviceLabelList.add(taServiceLabel);
                });
                taServiceLabelService.saveBatch(serviceLabelList);
                
                responseBean.addSuccess(taService);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taServiceAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 批量删除对象
     */
    @ResponseBody
    @RequestMapping(value="/admin/service/delete", method= RequestMethod.DELETE)
    public ResponseBean taServiceDelete(@RequestBody String[] ids){
        ResponseBean responseBean = new ResponseBean();
        try {
            List<String> idList = Arrays.asList(ids);
            if(iTaServiceService.removeByIds(idList)){
                responseBean.addSuccess("success");
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taServiceDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 修改对象
     * @param id  实体ID
     * @param taService 实体对象
     * @return
     */
    @RequestMapping(value="/taService/{id}",method= RequestMethod.PUT)
    public ResponseBean taServiceUpdate(@PathVariable Integer id,
                                        @RequestBody TaService taService){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTaServiceService.updateById(taService)){
                responseBean.addSuccess(taService);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taServiceUpdate -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/taService/{id}",method= RequestMethod.GET)
    public ResponseBean taServiceGet(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTaServiceService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taServiceDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
    
    /**
     * 分页查询列表
     * @return
     */
    @RequestMapping(value="/pc/service/list",method= RequestMethod.GET)
    public ResponseBean pcServiceList(){
        ResponseBean responseBean = new ResponseBean();
        try {
            QueryWrapper<TaService> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status",ConstantFoyo.STATUS_EFFECTIVE);
            queryWrapper.orderByDesc("sort");
            queryWrapper.orderByDesc("create_time");
            //使用分页插件
            List<TaService> list = iTaServiceService.list(queryWrapper);
            list.stream().forEach(e -> {
                QueryWrapper<TaServiceLabel> labelQueryWrapper = new QueryWrapper<>();
                labelQueryWrapper.eq("service_id",e.getId());
                labelQueryWrapper.orderByDesc("sort");
                labelQueryWrapper.orderByDesc("create_time");
                List<TaServiceLabel> labelList = taServiceLabelService.list(labelQueryWrapper);
                e.setServiceLabelList(labelList);
            });
            responseBean.addSuccess(list);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taServiceList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
