package cn.stylefeng.guns.zjxl.cnotroller;

import cn.stylefeng.guns.zjxl.model.ZjxlPartner;
import cn.stylefeng.guns.zjxl.service.ZjxlPartnerService;
import cn.stylefeng.guns.zjxlUtil.BaseController;
import cn.stylefeng.guns.zjxlUtil.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/***
 * @ClassName: ZjxlPartnerController
 * @Description: 公司官网-合作伙伴
 * @Auther: shi kejing
 * @Date: 2020/11/3 14:56
 */
@RestController
@Api(description = "公司官网-合作伙伴")
@RequestMapping(value = "/zjxl/partner")
public class ZjxlPartnerController extends BaseController {

    private Logger logger = (Logger) LoggerFactory.getLogger(ZjxlPartnerController.class);

    @Autowired
    private ZjxlPartnerService partnerService;

    @RequestMapping(value = "/findPartenrById", method = RequestMethod.GET)
    @ApiOperation("查询合作伙伴")
    public String findPartenrById(
            @ApiParam(name = "id", value = "partnerId", required = false) @RequestParam(value = "id", required = false)String id,
            @ApiParam(name = "name", value = "partnername", required = false) @RequestParam(value = "name", required = false)String name,
            @ApiParam(name = "pageNo", value = "第几页", defaultValue = "1") @RequestParam(value = "pageNo", required = false) Integer pageNo,
            @ApiParam(name = "pageSize", value = "分页大小", defaultValue = "10") @RequestParam(value = "pageSize", required = false) Integer pageSize){
        try {
            List<ZjxlPartner> partnerList = partnerService.findPartenrById(id,name,pageNo-1,pageSize);
            return write(200,"查询成功", "data", PageUtil.getPage(partnerList,pageNo,pageSize,partnerService.allCount()));
        }catch (Exception e){
            e.printStackTrace();
            return write(-1,"查询失败");
        }
    }

    @RequestMapping(value = "/addPartenr", method = RequestMethod.POST)
    @ApiOperation(value = "添加合作伙伴")
    public String addPartenr(@ApiParam(name = "partnerName", value = "合作伙伴名称", required = false) @RequestParam(value = "partnerName", required = false)String partnerName,
                             @ApiParam(name = "partnerImg", value = "合作伙伴图片", required = false) @RequestParam(value = "partnerImg", required = false)String partnerImg,
                             @ApiParam(name = "partnerSort", value = "排序", required = false) @RequestParam(value = "partnerSort", required = false)Integer partnerSort,
                             @ApiParam(name = "partnerIsLine", value = "是否上线", required = false, defaultValue = "0") @RequestParam(value = "partnerIsLine", required = false)Integer partnerIsLine){
        try {
            ZjxlPartner partner = new ZjxlPartner();

            List<ZjxlPartner> partners = partnerService.findAll();
            Iterator iterator = partners.iterator();
            while (iterator.hasNext()){
                if (partnerName.equals(iterator.next())){
                    return write(-1,"该合作伙伴已存在");
                }
            }

            partner.setPartnerName(partnerName);
            partner.setPartnerImg(partnerImg);
            partner.setPartnerSort(partnerSort);
            partner.setPartnerIsLine(partnerIsLine);

            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            partner.setPartnerCreateTime(df.format(new Date()));// new Date()为获取当前系统时间

            return write(200,"添加成功","data",partnerService.addPartenr(partner));
        }catch (Exception e){
            e.printStackTrace();
            return write(-1,"添加失败");
        }
    }

    @RequestMapping(value = "/updatePartner", method = RequestMethod.POST)
    @ApiOperation(value = "合作伙伴修改上线")
    public String updatePartner(String id, Integer isLine){
        try {
            if (id == null || isLine == null){
                logger.info("修改是否上线：id====="+id+"，isLine===="+isLine);
                return write(-1,"获取参数失败");
            }
            return write(200,"修改上线成功","data",partnerService.updatePartner(id,isLine));
        }catch (Exception e){
            e.printStackTrace();
            return write(-1,"修改上线失败");
        }
    }

    @RequestMapping(value = "/deletePartner", method = RequestMethod.POST)
    @ApiOperation(value = "删除合作伙伴")
    public String deletePartner(String id){
        try {
            if (id == null){
                logger.info("删除合作伙伴：id====="+id);
                return write(-1,"获取参数失败");
            }
            return write(200,"删除成功","data",partnerService.deletePartner(id));
        }catch (Exception e){
            e.printStackTrace();
            return write(-1,"删除失败");
        }
    }

    @RequestMapping(value = "/updatePartenr", method = RequestMethod.POST)
    @ApiOperation(value = "编辑合作伙伴")
    public String updatePartenr(@ApiParam(name = "partnerId", value = "合作伙伴id", required = false) @RequestParam(value = "partnerId", required = false)String partnerId,
                                @ApiParam(name = "partnerName", value = "合作伙伴名称", required = false) @RequestParam(value = "partnerName", required = false)String partnerName,
                                @ApiParam(name = "partnerImg", value = "合作伙伴图片", required = false) @RequestParam(value = "partnerImg", required = false)String partnerImg,
                                @ApiParam(name = "partnerSort", value = "排序", required = false) @RequestParam(value = "partnerSort", required = false)Integer partnerSort,
                                @ApiParam(name = "partnerIsLine", value = "是否上线", required = false, defaultValue = "0") @RequestParam(value = "partnerIsLine", required = false)Integer partnerIsLine){
        try {
            ZjxlPartner partner = new ZjxlPartner();
            partner.setPartnerId(partnerId);

            List<ZjxlPartner> partners = partnerService.findAll();
            Iterator iterator = partners.iterator();
            while (iterator.hasNext()){
                if (partnerName.equals(iterator.next())){
                    return write(-1,"该合作伙伴已存在");
                }
            }

            partner.setPartnerName(partnerName);
            partner.setPartnerImg(partnerImg);
            partner.setPartnerSort(partnerSort);
            partner.setPartnerIsLine(partnerIsLine);

            return write(200,"添加成功","data",partnerService.updatePartenr(partner));
        }catch (Exception e){
            e.printStackTrace();
            return write(-1,"添加失败");
        }
    }

}
